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.
Related
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.
I'm new to iOS development and I'm using Swift in my current project.
I'm trying to achieve something similar I did in Android. I'm trying to run a background service when the app is closed/minimised that will get executed every 2 seconds to keep the user informed that the app is still running in the background and checking for status with the server.
How can I achieve that? I've tried using the "background fetch" method but it doesn't get executed every 2 seconds.
The background service will run no longer than 5 minutes if it matters.
No you can't do that. What you can do is, call a service in applicationDidEnterBackground that lets your server know that your app is in background or killed(You will get very less time for this to get executed, approx. 3-5 sec.).
An Android app like the one you described will likely drain the device’s battery prematurely. This is the simple reason why Apple has restricted background execution to a clearly defined set of tasks:
Apps that play audible content to the user while in the background, such as a music player app
Apps that record audio content while in the background
Apps that keep users informed of their location at all times, such as a navigation app
Apps that support Voice over Internet Protocol (VoIP)
Apps that need to download and process new content regularly
Apps that receive regular updates from external accessories
This list is taken from Apple’s official description of background tasks: Background Execution.
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
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.
I have always coded for Android, and now I'm looking to expand my knowledge to iOS development; so I'm really new at this, please be patient.
I understand that only a small group of apps are allowed to run indefinitely in the background. Those are VoIP, Music players and location tracking apps.
I want to write a chat app using the XMPP framework. Everything is fine until the user puts the app in the background, in which case, the app will stay connected for about ten minutes to then be killed by the system and therefore the user won't be able to receive new messages.
I am aware of hacks to keep the app alive. Hacks such as defining it as a music playing app in the info.plist file and then just play some empty sound indefinitely. But I'm also aware that Apple will reject the app when it's time to publish to the App Store.
So, normally, how do other apps do it? How can other chat apps stay alive in the background to receive new messages from the servers? Apps like Google Hangouts, IM+ and such?
Ideally, they aren't really running in the background, but use push notifications, as others have mentioned.
But some chat clients seem to do something else: I've verified (by sniffing the traffic of an idle iOS device) that at least Google Hangouts, Facebook and Skype all keep a persistent socket opened in the background, and regularly send traffic to keep it alive.
I'm suspecting that they are using the VoIP exceptions to Apple's otherwise strict background execution policies. iOS allows "VoIP apps" to run in the background and keep one socket open to be notified about incoming calls and messages.
Maybe they are also using the new "background fetch" feature of iOS 7, but as far as I know, that doesn't allow persistent socket connections.
The iOS operating system allows for the existence of something called a PUSH NOTIFICATION
There exists hundreds of tutorials online which teach you how to implement the notification code and how to respond accordingly when you receive such a message!
http://www.raywenderlich.com/32960/apple-push-notification-services-in-ios-6-tutorial-part-1
Check this link out for an in-depth tutorial on push notifications!
http://maniacdev.com/2011/05/tutorial-ios-push-notification-services-for-beginners
I think most of these apps use push notifications and just load the last messages from the server as soon as the app is being opened.
While there are some hacks, and your app can ask for more time when it goes in background (up to a point, and with no guarantees), this is a perfect application for push notifications.
The server tells the phone there's a message, and iOS wakes your app up to process it.
https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Introduction.html
As of iOS 7 there is a new background-execution mode - 'fetch' for apps that need to periodically fetch new data. It sounds like your case would meet that definition.
You can find the information in the iOS App Programming Guide -
Fetching Small Amounts of Content Regularly
In iOS 7 and later, an app that retrieves content regularly from the
network can ask the system for background execution time to check for
new content. You enable support for background fetches from the
Background modes section of the Capabilities tab in your Xcode
project. (You can also enable this support by including the
UIBackgroundModes key with the fetch value in your app’s Info.plist
file.) At appropriate times, the system gives background execution
time to the apps that support this background mode, launching the app
directly into the background if needed. The app object calls the
application:performFetchWithCompletionHandler: method of its app
delegate to let you know when execution time is available.
You can also use push notifications, but that requires some server infrastructure
An app running in the background has limited capability. Read App States and Multitasking thoroughly to decide how best to design your app. Chat is not listed as one of the specific exceptions that can operate with a more relaxed policy. You will never be able to "keep [your] app live in background forever." You might be able to leverage an iOS 7 feature also described in this guide, Fetching Small Amounts of Content Regularly.
iOS App Programming Guide: App States and Multitasking
https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOS ProgrammingGuide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html