1

Is is possible to perform some specific task after some specific time when app state goes to terminate/not running state.

func applicationDidEnterBackground(application: UIApplication) {
    print("APP in background state") // Working fine
    NSTimer.scheduledTimerWithTimeInterval(5.0, target: self, selector: "backService", userInfo: nil, repeats: true)

}

But when app state goes to terminate state then its not working?

func applicationWillTerminate(application: UIApplication) {
     NSTimer.scheduledTimerWithTimeInterval(5.0, target: self, selector: "backService", userInfo: nil, repeats: true)
}

I also tried

func applicationWillTerminate(application: UIApplication) {

    let application = UIApplication.sharedApplication()
    self.applicationDidEnterBackground(application)
    application.beginBackgroundTaskWithExpirationHandler { 
                    NSTimer.scheduledTimerWithTimeInterval(5.0, target: self, selector: "backService", userInfo: nil, repeats: true)
    }

}
Haseeb
  • 361
  • 2
  • 20

2 Answers2

0

schedule a local notification, cancel and reschedule as long as your app is running

then if it is closed, the notification isn't cancelled anymore and will fire!

Daij-Djan
  • 49,552
  • 17
  • 113
  • 135
  • Notification will fire i agreed but i want to trigger after some specific time on terminate state.Not at a time of terminating state. – Haseeb Feb 16 '16 at 14:33
  • "Send local notification after termination app swift 2" ;) thats the best I can come up with. – Daij-Djan Feb 16 '16 at 14:48
0

You're not actually scheduling a notification... When you create a new notification locally, which can be done in the applicationWillTerminate method, there's a property called fireDate which you can set to have the notification get sent whenever you want to.

pbush25
  • 5,228
  • 2
  • 26
  • 35
  • Notification send at the time of terminating but I want to send after some specific time while app in terminating state. – Haseeb Feb 16 '16 at 15:58
  • Then set the fire date to whatever arbitrary time you desire. That's the whole point of that property. – pbush25 Feb 16 '16 at 15:58