0

In my app the user can planify an event, thanks to a timer. I want this timer to work even if the app is in background. How can I do that ?!

[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerFired) userInfo:nil repeats:YES];
Lou
  • 277
  • 1
  • 5
  • 15
  • This question has been asked before. Look at this response: [click here][1] [1]: http://stackoverflow.com/questions/22628922/how-to-run-nstimer-in-background-beyond-180sec-in-ios-7 – Wyetro Jul 15 '14 at 13:14
  • I think local notifications will do this – Starbax Jul 15 '14 at 13:14
  • When apps are going into the background all timers are canceled. You should use `UILocalNotification` for this. When your app is in the background it may not run any code, unless it fall into one of the background running modes. Since Apple is trying to increase battery life you should not keep your app running in the background longer then needed. So if you want to inform the use about an upcoming event use `UILocalNotification` – rckoenes Jul 15 '14 at 13:14
  • i have already seen theses post. A notification isn't useful to launch code, does it ? – Lou Jul 15 '14 at 13:20
  • Can you do some more clarification of your objective..?? – Alex Andrews Jul 15 '14 at 14:33
  • Well, I want to be able to launch a code after a period of time, potentially higher to 10 minutes, even if the app is in background. It seems difficult to do it ! – Lou Jul 15 '14 at 14:44

1 Answers1

0

If you want to trigger some actions after a period even if the app is in background Local notifications can help you.

Refer https://developer.apple.com/library/ios/DOCUMENTATION/NetworkingInternet/Conceptual/RemoteNotificationsPG/Introduction.html

And there is tutorial here http://www.appcoda.com/ios-programming-local-notification-tutorial/

Note : NSTimer will stop on app entering background

Alex Andrews
  • 1,498
  • 2
  • 19
  • 33
  • Thank you ! but actually the goal of my app is to launch a code when the user isn't using the phone ... So a notification isn't suitable ! – Lou Jul 15 '14 at 14:13