Perform Background Tasks in iOS - 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

Related

IOS Real time updates won't trigger if app is in background state

I set up websocket support in my hapi server using nes
Tried it on android and so far the realtime updates is still working even in background state.
The problem is with IOS, realtime update won't trigger when your app is in the background state. How can i make this work
iOS only allows your app to keep running in the background in some specific cases. Keeping the WebSocket connection is not one of them. In order to achieve this realtime update, your server needs to provide an API which allows the app to fetch the new data, and your app needs to register a background task and do the fetch. For more detail, you can check the link here.

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.

Background fetch in iOS

Coming from Android development where we have background services that run nicely its a little hard for me to start thinking the Apple way.
What we achieved in Android was that our background service keeps updating our database at regular interval which can than update the UI if they are in foreground or when they user start the app.
Things look a little different in iOS as there is nothing similar to Background Services. Here are my question
Is there ANY way that my background fetch run even if my app is terminated by user? I am not using HTTP to download content so I am not sure if Background Transfer Service API would help.
Is there a possibility to schedule multiple fetch operations at varying times?
Or do I have to completely rethink how my services work for iOS development?
Any idea would be appreciated.
If you google "background fetch iOS", you'll see tons of links. Bottom line, effective iOS 7, Apple introduced background fetch (see the Fetching Small Amounts of Content Opportunistically section of the App Programming Guide for iOS: Background Execution). This is designed for an app to be able to periodically check to see if there is more data to download.
iOS 7 also introduced NSURLSession and background NSURLSessionConfiguration, which allows you to initiate a series of requests that will continue even after the app is terminated.
For general counsel on background operations, see the aforementioned App Programming Guide.

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

How to periodically wake and check status in iOS?

I want my app to periodically wake and check a status, but burn minimal cycles between wake ups. Is the a Core call for this or an accepted design pattern?
Prior to ios7, apps could only ask for execution cycles in background for a handful of purposes (e.g. playing sound or getting location updates). While they added more options in ios7, like periodically fetching data from a server, I'm still not clear there is a way to do this. (It's also a bit difficult for me to imagine why your app cares about battery if it isn't running, but maybe that is my failure of imagination.) Recommend reading Background Execution and Multitasking in the AppleDocs.

Resources