1

So I've read that in iOS, all timers will pause when your app is running in the background. I've also read that you can run tasks in the bg using beginBackgroundTaskWithExpirationHandler (like so).

What I am trying to achieve is to call a method once every 3 minutes, and another method a fixed-amount of time before the first one. I have managed to do this within one NSTimer which repeats in a way that lets me do this. It works fine but is obviously disabled (or paused) when the app is in the background - I can only assume because of the reason described above.

Does anyone know if there's a way to run a timer or at least call something after a specific amount of time so I can do this?

Community
  • 1
  • 1
ingh.am
  • 25,981
  • 43
  • 130
  • 177
  • 1
    FYI - Currently `beginBackgroundTaskWithExpirationHandler` is limited to 10 minutes by Apple, this means you will only get 3 calls in. `UILocalNotification` is unable to call any code, you will have rely on the user opening your application once the notification fires. – Joe Jul 25 '12 at 15:19
  • 1
    There are only a few ways of having your application continue to run in the background (for longer periods of time) - mainly because they are receiving location updates or are playing media. – ChrisH Jul 25 '12 at 15:41
  • The ten minute limit is plenty, however could I say start beginBackgroundTaskWithExpirationHandler with the intention to end it at around 3 minutes in and then start a new one? Would that work? – ingh.am Jul 25 '12 at 19:49
  • Thanks I removed the question about `UILocalNotification` as I realize it's not quite right for the job now! – ingh.am Jul 25 '12 at 19:52
  • Did you ever find a way to resolve this? I am running into a similar problem with an audio application. – Matthijs P Jun 30 '13 at 09:44

1 Answers1

2

Basically if you want to continue running active in the background you have to meet one of the following requirements. From the Apple docs:

Implementing Long-Running Background Tasks

For tasks that require more execution time to implement, you must request specific permissions to run them in the background without their being suspended. In iOS, only specific app types are allowed to run in the background:

  • Apps that play audible content to the user while in the background, such as a music player app
  • Apps that keep users informed of their location at all times, such as a navigation app
  • Apps that support Voice over Internet Protocol (VoIP)
  • Newsstand apps that need to download and process new content
  • Apps that receive regular updates from external accessories

Apps that implement these services must declare the services they support and use system frameworks to implement the relevant aspects of those services. Declaring the services lets the system know which services you use, but in some cases it is the system frameworks that actually prevent your application from being suspended.

http://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html#//apple_ref/doc/uid/TP40007072-CH4-SW24

ChrisH
  • 904
  • 6
  • 13
  • Thanks for the link. Basically we have built a tracking application which is live in the app store, however I'm just looking into some ways of turning the tracking off inbetween messages as some users will have it track in less often, such as once every 5 minutes. The problem I come across is that if I turn the tracker off (`stopUpdatingLocation`) then all of a sudden nothing will run in the background. As soon as I start listening again the background stuff all starts to work again. – ingh.am Jul 25 '12 at 19:52
  • For battery reasons? If you're using location updates, then I guess your only option is to turn down the accuracy requirements, assuming that iOS will then moderate it's GPS usage. The documentation suggests kCLLocationAccuracyThreeKilometers will primarily use wi-fi and mobile masts. – ChrisH Jul 25 '12 at 20:19
  • We've tried this and it's pretty awful in comparison. For example I work near Hull and it always puts me on the other side of the river which is quite far away! While I accept that battery usage is always going to be an issue with this I've just been trying to limit the amount of time that a) the GPS is on and b) the app is "background active" if you know what I mean. I'm just trying to run some timers at the moment to see if I can at least get my concept working. – ingh.am Jul 25 '12 at 20:24
  • Yeah, I've had that same experience. You can tweak the accuracy on the fly, so I meant rather than turning off location updates as you have attempted to, just turn down the accuracy, and then back up again when you want to. – ChrisH Jul 25 '12 at 20:26
  • Ahh, I like that idea. I'll try that too. – ingh.am Jul 25 '12 at 20:53