0

I have got NSTimer in my app checking in the background if it's needed to post a local notification, and it works pretty fine, but only for some minutes after entering the background mode. How can i make that NSTimer work all the time app is in the background?

[[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:nil];
[[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
serg_ov
  • 563
  • 7
  • 18

1 Answers1

1

This is only possible if you register for Long-running background tasks and needs to have a specific reason to get approval. Have a look at the documentation

Christian
  • 4,596
  • 1
  • 26
  • 33
  • I know that! I ask how to register for Long-running background tasks – serg_ov Mar 11 '14 at 20:59
  • I can describe my situation more sophisticated: I'm making a boxing timer which plays a gong sound at the end and start of each round (can look for some boxing timer in the appstore). I need my app to work in te background. How can that be emplemented? – serg_ov Mar 11 '14 at 21:06
  • Did you put as mentioned in your Info.plist the key `UIBackgroundModes` with value audio? – Christian Mar 11 '14 at 21:36
  • Yes i have, but timer still killed after some minutes – serg_ov Mar 12 '14 at 03:24
  • look [here](http://stackoverflow.com/a/13789070/1505074) - he has done exactly what you want and to keep the app alive after the music is finished he used an NSTimer – Christian Mar 12 '14 at 07:51
  • Not working solution ;( It works but only for some minutes. maybe i'm writing something not right... Should i invalidate the background task before starting it again? – serg_ov Mar 13 '14 at 18:41