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

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 :
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.
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.
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!

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.

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.

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

Related

Perform Background Tasks in iOS

I am trying to develop an iOS application where I need to perform most of the operations from background even after the app is terminated in a particular time interval like fetching the below details and sending it to server.
HealthKit data
Battery level
Accelerometer data
I found some solutions like Background App Refresh and Silent push notification. But which is a better solution for my need. Also please suggest if there is any other better approach to achieve it.
This is all done in the background. You can consult this article for the latest changes in background tasks for iOS - All about handling Background tasks in iOS 13

Periodic background synchronization

Im quite new to iOS programming and now want to implement a periodic background synchronization to synchronize my server data with client data. What I want to achieve is comparable with Androids SyncAdapter where you can define a time interval (for example each 30 minutes) and the system will trigger the defined task automatically in the background.
Until now I could not find such mechanism for Swift 3.0 so I need to ask if somone has experience or some hints for me how I can achieve this.
What I want to do sounds quite simple:
When the app starts for the first time the app should setup a sync manager which automatically triggers a background task every 30 minutes. The background task is responsible to synchronize server and client data (using Alamofire).
How can I do that?
There is an iOS feature called BackgroundFetch, which you can set up for
regularly downloads and processes small amounts of content from the network
You can setup a minimumBackgroundFetchInterval.
In contrast to the mentioned Android feature, this interval is not guaranteed though.
The OS does some heuristic in a blackbox. It rewards you for using a "reasonable" (to the OS) CPU time/ power consumption and also for being used often by the user. On the other hand you get punished for draining the battery or (even worse) never being used/opened by the user.
See: Apple Sample and Apple Docs
Update: Since iOS13, BackgroundFetchis deprecated.
There is a similar, new API named BGTask, BGAppRefreshTask is the equivalent to deprecated BackgroundFetch.
See Apple Docs
Alternatively, depending on your needs, you can post a Silent (push) Notification whenever the users data changes on server side. A silent push wakes up your app without notifying the user, so you can fetch data and maybe inform the user by scheduling a local notification.
See: Apple Documentation
You can't. Apple doesn't allow 3rd party apps to have regular background time like that. You'll need to come up with another approach like implementing a silent push notification from your server when new content is available.
As #ekscrypto points out in their comment, you can use Background fetch to load small amounts of data when the system decides to fetch it. However, you don't have any control over when that fetching takes place. Search on "Fetching Small Amounts of Content Opportunistically" in the Xcode help system for more information.

Background fetch alternatives

I want to my app do some computations and then communicate with external server via HTTP. I would like to perform this operations both in foreground and background. It seems that Background fetch mode is best choice for me but I have some concerns related with this. What is the minimal time interval between fetches? I read somewhere this is 10 min, is that true? I read also that when user force quit fetches are no longer invoked. Is there any walk around to this? Finally, is there any alternative to background fetch? I saw there is Newsstand mode what looks promising. Can I use it for my purposes?
If you use background fetch or another background mode and don't really use it for the intended use, iOS will detect that an kill your app. (the most promising mode for that would be VOIP, but this would't make it into the App Store, as it's a cheat either)
You can start a background task, when your app enters background, what gives you 3 minutes time (iOS 7 and above).
As I did it before, you can schedule a local notification and use it to remind the user, that he should bring the app back into the foreground for more calculations, if he likes.

swift/ios refreshing app data when in background

I'm writing a iOS/Swift application which reads data from a REST service each X minutes and updates the UI accordingly.
Now I would like that when the app is put in the background, a task keeps being invoked at X minutes intervals reading from the REST service and, in case the data just read satisfies a given condition, show a notification prompting the user to bring the app back to the foreground.
In my searches I've read that during applicationDidEnterBackground event, I should start a task with beginBackgroundTaskWithExpirationHandler.
The problem is that, if I've understood correctly, this allows a maximum of 10/15 minutes after which the app is terminated if the task is not stopped with endBackgroundUpdateTask, while I want the task to keep polling the service indefinitely (at least until the user disable it from the app's settings)
My question is:
How is this kind of functionality performed normally? Do some common solutions or best practices exist for the solution of such a problem?
Use iOS Background Fetch feature where you can specify minimum background fetch interval. But actual interval between successive invocation of your code will be determined by iOS framework. For details checkout this link: http://code.tutsplus.com/tutorials/ios-7-sdk-working-with-background-fetch--mobile-20520
I use this approach in my app and I think it is a preferred way of doing.
You can use a local notification that can be presented from the background in case your condition is met.
Correct, iOS will eventually shut down the background process, you can't enforce continuous background activity. Use the backgroundTimeRemaining property to check how much time your application has left and try to handle it as gracefully as possible by calling endBackgroundTask so that iOS does not force kill your app.
As a solution, you could think about using remote notifications with with content-available : YES, which runs the didReceiveRemoteNotification
Have a look at the Parse.com Their local datastore is an abstraction for what you are trying to acheive.
By the way, is it really necessary to refresh in the background. If call is relatively quick, there is no need to refresh until the user open's the app. Background processes like that, using the net can be quite battery consuming when the user are not on a Wifi. So consider the use case carefully!

Call web service even if ios app is in background

In my application I need to log user status every 15 minutes. Is it possible if the app is in background (not killed)??
Currently I enables the Background location update. But how to call in each and every 15 minutes
While other answers are correct about background fetch, there is also another background option called VoIP, which apps like Skype use. In this case OS will wake up your application more frequently (even every 10 minutes if I remember correctly) and you can keep pinging your server in background. The obvious down side is that your app must have Voice over IP functionality, otherwise it would get rejected on the App Store.
All you need to do is add call setKeepAliveTimeout:handler: method and have voip enabled in background capabilities.
Just adding this for the sake of completion.
You may want to look at the Background Fetch capabilities added in iOS7 and beyond:
http://www.appcoda.com/ios7-background-fetch-programming/
https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html (Search for background fetch)
You won't be able to control the interval to exactly 15 minutes but you will find that this mechanism is the closest Apple will allow.
It is not possible to perform network calls at clearly specified intervals when your app is in background mode. You can tell your app to use background fetches
This article gives a great overview on the capabilities that you can do with this API. Basically it allows you to tell iOS that your app wants to perform networking calls in the background, however you can't exactly control when and how often iOS the network calls are going to be performed. iOS will schedule your network calls for you, and depending on how your app is used it will adopt the frequency with which it performs the requests. Documentation can be found here.

Resources