Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Working with UILocalNotifications, Remote Notifications with completion handler and Background Fetch

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!

like image 226
mayuur Avatar asked May 07 '14 03:05

mayuur


2 Answers

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.

like image 88
Léo Natan Avatar answered Oct 12 '22 00:10

Léo Natan


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.

like image 40
Anil Kumar Avatar answered Oct 12 '22 01:10

Anil Kumar