7

With iOS7, it is possible to have completionHandler for Remote Notifications. Is it possible to do the same for UILocalNotifications?

Basically, I want a webservice to post my some data at regular time intervals of 30 seconds, even if the app is in background. For this I considered 3 options but didn't get help from any :

  1. Background Fetch : This will work in background, but I can't use it as it is not mandatory that iOS will always invoke this background fetch at my desired time intervals.

  2. Remote Notifications : This works perfectly. But every 30 seconds I have to post a Remote PUSH Notification, which is not at all practical. Also it'll be great if I could handle it locally.

  3. UILocalNotifications : There's no completion handler for this. User WILL HAVE TO open the app. So this ain't working as well!

Are there any other options? Or even with iOS7, it's still not possible to do something locally in background?

Please help. Thanks!

mayuur
  • 4,736
  • 4
  • 30
  • 65
  • Why do you need to send information to the server so often if the user is not even looking at your app? – Rivera May 13 '14 at 01:50
  • @Rivera its something which the user will start recording and it should continue even if the app is in background. it stops only when user stops it. weird requirement, i know! but that's what i require to build! – mayuur May 13 '14 at 17:53
  • Recording what? Maybe you can record locally? Apple will check how much your App consumes and adjust how often it calls your background method, so you better be "eco" by not sending the data to the server just yet. – Rivera May 14 '14 at 03:22

3 Answers3

5

You covered all the options, and as you see, this isn't supported. For good reasons, mostly, as waking up the application in the background is a costly operation (in iOS7, a snapshot is taken after apps finish their background work), doing it every 30 seconds would be devastating to the battery life.

Seeing as you haven't mentioned what data you need to post, in most cases I would suggest you redesign your app to be friendly to your users' batteries. If you need location reporting, consider listening to significant location changes instead of recording every 30 seconds.

Abusing push notifications is possible (note, that silent remote notifications are rate-limited by Apple), but consider the experience your users will have.

If you feel that this feature should be there and is missing, you should open an enhancement request with Apple and post the radar number here so people can duplicate it.

Léo Natan
  • 56,823
  • 9
  • 150
  • 195
2

Background fetch is a direct answer for your problem. Background fetch initiates your fetch handler whenever the iOS is free to execute a task periodically. All you need to do is initiate you NSURLSession request in

- (void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler

NSURLConnection is no longer a valid API for service calls or nor it supports background tasking. NSURLSession is clearly a replacement API for NSURLConnection with its advanced iOS 7 benefits. Adding below, documentation from Apple's iOS documentation

 NSURLSession is a replacement API for NSURLConnection.  It provides
 options that affect the policy of, and various aspects of the
 mechanism by which NSURLRequest objects are retrieved from the
 network.

Background fetch interval can be set to define the repetition frequency for background fetch, but it also considers factors of the OS resources and pending operations

- (void)setMinimumBackgroundFetchInterval:(NSTimeInterval)minimumBackgroundFetchInterval;

iOS 7 clearly gives a better way to update content of the application, but it respects device resources & user priority as iOS generally does. Hope this answers your question.

Anil Kumar
  • 654
  • 1
  • 6
  • 18
1

Actually, all looks like you can't communicate with webservice at regular time intervals from background.

UILocalNotifications or Remote Notifications need user's action to wake up the app if it's backgrounded. EDITED: Starting from iOS 7.0 remote notification can wake up the app but it's not a flexible solution

iOS allows more activities in background for applications that use one of specified UIBackgroundModes, see please "Table 3-4 Background modes for apps":

Here is a link to related Apple docs

If your application isn't positioned for one of bg modes directly, I agree with Anil Kumar (post above) that background fetch is the most useful thing here. However it doesn't do completely what you need. [UIApplication setMinimumBackgroundFetchInterval:] it doesn't mean any strict time. It means minimum interval only (or desired time). Here is more info:

performFetchWithCompletionHandler never gets fired

Thanks

Community
  • 1
  • 1
Alex Peda
  • 4,210
  • 3
  • 22
  • 31
  • 1
    Regarding remote notifications, that i incorrect. If the user has not specifically killed the app using swipe in the task manager, remote notifications can be used to wake the app in the background, provided the push payload arrives at correct intervals. But it's a bad idea. – Léo Natan May 17 '14 at 12:03
  • @LeoNatan Agreed, starting from iOS 7.0 remote notification can wake up the app. Thanks for clarifying. – Alex Peda May 17 '14 at 12:19