Running an application in the Background? - ios

My application runs in the Background (getting location updates) which I need to push to my server for every 10 seconds.
I have scheduled a timer which invokes a function in which the current location updates are captured and pushed to the server. This is running smoothly if the app is there in the foreground. When the app is moved to the background this functionality is running for 15 minutes after which I cannot see the method being invoked at all.
I know if an application is put into background it will be put into suspended state at any time. Also if another app running in the foreground requires memory at that time iOS may terminate some applications in the background. But in my case no application is running in the foreground as I have locked my device.
I also have an idea about expirationHandler. Would like to know if I can keep calling the function in the background without my app going into suspended state and Apple should accept that.
Any suggestions are welcome.

You can add App registers for location updates under Required background modes in your plist.

The same scenario was also in my application i have set the uibackground mode in plist file and use that service from appdeligate and apple approved that application :)
Hope it may help you.

Related

What are the different options for running code at a specified time in iOS?

What are the different options for running code on an iOS device at a specified time while the app is in the background? So far I have found that I could possibly use a Timer object, use the Grand Central Dispatch timer, and use CloudKit Push Notification with the Apple Push Notification service.
Any ideas are welcome. I'm having a hard time finding the solution.
Here is the apple documentation for background execution.
Here is the apple documentation for the execution states of an application.
You should understand a few things:
When an app moves into the background you have a very small amount of time (3 minutes approximately) to execute finite-length tasks.
If your application is not running, you could use a notification to wake the application. This requires the user to take an action and will make the application active.
You can also wake an application with a local notification, it doesn't have to be a remote notification.
The code that you want to run really needs to fall into one of the blessed scenarios that apple defines otherwise you run the risk of being deprioritized or ignored entirely.
A block of code scheduled to run with a Timer or GCD will not be invoked while the application is in the background.
Roughly, you should register a background execution mode and follow the guidelines I linked above and while your application is backgrounded the application will be given time to execute code.

NSURLsession background transfer service for download task, can it work even when App is suspended?

Before asking a question, let me clarify what I understand about iOS App states:
Backgrounded: In this state, your app is not in the foreground anymore but it is still able to run code.
Suspended: Your app enters this state when it’s no longer able to run code.
Now, I wanted to keep downloading going on when App is in background, i.e. it still does exist in multitasking screen. It is working as expected with background transfer service.
But, in some tutorial reference, I have read that you can perform downloading even when App is Backgrounded / Suspended. Can it work even when my App is suspended, i.e. removed from multitasking screen ?
I have been reading many documents including Apple class reference regarding background transfer service with download task, but no one clarifies that the download will not work when App is suspended (killed).
Appreciate your thoughts and advices !!!
If your app has been suspended by the system (without force quiting from multitasking screen) your background session will continue to work.
If you force quit the application all download tasks will be canceled.
The following is from backgroundSessionConfigurationWithIdentifier(_:) documentation :
If an iOS app is terminated by the system and relaunched, the app can use the same identifier to create a new configuration object and session and retrieve the status of transfers that were in progress at the time of termination. This behavior applies only for normal termination of the app by the system. If the user terminates the app from the multitasking screen, the system cancels all of the session’s background transfers. In addition, the system does not automatically relaunch apps that were force quit by the user. The user must explicitly relaunch the app before transfers can begin again.
Apps displayed in the multitasking UI aren’t necessarily executing code or fetching data. Listed apps may be suspended or not running at all
[[UIApplication sharedApplication] applicationState] will check your application state, you can test your app.
NSURLSession class can hand off downloads and uploads to the operating system when the app becomes inactive. As with almost all background execution APIs, if the user force quits from the multitasking UI, the background operation will terminate
In iOS 7, Apple added support for background fetch—a kind of smart, per-app crontab that wakes up at opportunistic times. There is no way to force background fetches to execute at exact intervals. iOS checks how much data and battery power was used during previous background fetches when scheduling future callbacks.
Background fetches can also be triggered by remote push notification and have a very similar delegate method with the same completion handler.
Full Tutorial is here
https://blog.newrelic.com/2016/01/13/ios9-background-execution/

GPS Application running in the background

I have an IOS application that uses location services.
I know that applications using location services can execute in the background.
However, I don't want my app to run in the background. If the user isn't watching it, it should not power.
Is this going to happen naturally (i.e. something special is needed for apps using location services to continue to operate in the background?)?
If not, what do I need to do to ensure the app using location services suspends when it goes into the background? If so, what? And what would need to be done to restore the app?
Thx
No problem - just do nothing. When the user switches away from your app (to another app, or locking the screen) your app will be suspended.
However, to make absolutely sure you don't do any location in the background, use the applicationDidEnterBackground and applicationWillEnterForeground notifications (or the corresponding app delegate methods) to stop and restart location updating.

Is there any way to wake my application from killed state(killed from app switcher)?

I am creating an application that sends updated location to server regardless of app state. I have tried silent push to wake my application in background but it didnt work (it works fine except if my app is killed by user from app switcher). I am using location manager for getting current location.
Does IOS7 provides this feature to launch your application in background (even if app is killed by user from app switcher)??
I have spent five days without any success on this. If anyone can help me that would be highly appreciable.
Thanks in advance!!
If you need precise GPS locations you can set add the "location" value to the UIBackgroundModes array in your Info.plist as described in "UIBackgroundModes location and significant location changes with region monitoring". If you need only approximate locations you can use the startMonitoringSignificantLocationChanges method of CLLocationManager. As it is written in the documentation: "If you start this service and your application is subsequently terminated, the system automatically relaunches the application into the background if a new event arrives."
You can wake your app by using significantlocationchange.
You have to start significant location change while you switch your app to background.
Then system will wake your app after significant location change.
The frequency is not faster than 5 min and for every 500 meters.
You will have to catch the event by checking for LocationKey in didFinishLaunchingWithOptions in projects AppDelegate.

Sending Application to background on iphone

Is it possible to send the application to background programmatically on iPhone?
Under iOS 4.0, your app will be put in the background if another app enters the foreground, and there are enough resources to keep your app in the background. You could send a URL to Safari, or another app that has registered for a handler, and hope that there's enough memory (etc.) that the OS puts your app in the background after it starts Safari (or whatever app handled the URL). If you are lucky (which typically happens fairly often), you will have sent your app programmatically to the background.
Of course, whether your app runs in the background, or is just suspended, depends on other things (which you have to register with iOS 4.x for).
Apple does allow apps to exit (kill themselves) programmatically. But it's meant only as a last resort for when something bad happens from which your app cannot recover.
On all other occasions, apps are expected to stay in the foreground and running until either the user presses the home button or the app opens another app, with the users consent.
Actually you don't want to kill the app, however moving it to the background and returning to the home screen would leave a very similar impression. That's why I don't think Apple would approve it and consequently doesn't offer, as far as I know, any means to move an app to the background programmatically without opening another app.
No it is not possible. Only iOS can put your application in the background.
Note that there's a lot of confusion between "background" and "inactive". An inactive application is one that is not displayed on the GUI but is still running.
Technically an application in the background isn't running; it's dormant. Depending on the mode used, a background applications can receive signals and "wake up." See Background Modes for more details: https://developer.apple.com/library/ios/documentation/general/Reference/InfoPlistKeyReference/Articles/iPhoneOSKeys.html#//apple_ref/doc/uid/TP40009252-SW22
But a background application can also get terminated at any point without warning. As a matter of fact I always treat a background application as terminated. Therefore you should always clean up your application before you enter the background, and re-initialize it when you enter the foreground.
This is a must-read on this topic: https://developer.apple.com/library/ios/documentation/iphone/conceptual/iphoneosprogrammingguide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html
check out these post
local notifications?
iPhone - Backgrounding to poll for events

Resources