Launching terminated app by background notification - ios

Is it possible to launch a terminated app by a silent/background notification and keep it active past the 30s time limit?
I've been testing this for a while, but seems like no matter what is being done, app gets terminated once the 30sec time limit is reached or fetch handler returns a result. Starting a background location update doesn't prevent app from being terminated. Location updates are being generated.
Is there a way to keep app running indefinitely? Apple documentation doesn't mention anything about what can/cannot be done. Any idea would be greatly appreciated.

Related

How to kill app in background after X Amount of time

I am working on an app where I am getting an access token which would be valid for 300 seconds.
If in case app moves into the background (say phone call) for more than 300 seconds, I have to kill the app in the background.
I am able to increase the time of the app in the background using any services (Location etc) but please guide me how to kill the app in the background once token time (300 seconds) has been expired.
Have a look at this link.
https://www.raywenderlich.com/143128/background-modes-tutorial-getting-started [Section: Performing Finite-Length Tasks]
The example tells you how to get extra time to complete what you’re doing when the app gets into background.
An app can be killed using
exit(0)
This would lead to exiting the RunLoop, but it may lead to AppStore rejection.

How long does "location updates" process can run in background?

Assumes that I registered "location updates" service. If the location changes significantly, the callback function gets invoked.
Question: How long is my function allowed to run in the background? Let's say the function does so much work and need 10 minutes to finish, will it be terminated by system?
I guess Apple won't allow long-run job in location-updates-callback. But I couldn't find the official explanation. Could anyone help? thanks a lot.
https://developer.apple.com/library/content/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html
Your app will receive location updates till it stays in background. There's a chance that your app would go inactive when other active apps require memory. In this case, your app will stop receiving location updates. There's no fixed background execution time mentioned anywhere in Apple's documentation. It may be possible that your app can receive location updates for more than 10 minutes but only if it stays in background.

Reasons why iOS will terminate an app

We have a GPS tracking type iOS app which runs in the background. We are seeing infrequent cases where the app appears to be getting terminated with the applicationWillTerminate method being called while the tracking process is taking place.
We have more or less ruled out low RAM as the reasons. Also definitely not a battery level issue or users terminating the app.
Any suggestions what would be the next most likely causes for iOS to terminate the app?
Some thoughts we had were CPU usage, phone temperature level or simply because the app is running in the background for extended periods (e.g. days or weeks).
Are any of these likely or even possible causes for iOS to terminate the app?
From Apple's Background Execution docs:
Do minimal work while running in the background. The execution time
given to background apps is more constrained than the amount of time
given to the foreground app. Apps that spend too much time executing
in the background can be throttled back by the system or terminated.
Also, I assume you've verified you're not running afoul of the below (from the same doc):
Each call to the beginBackgroundTaskWithName:expirationHandler: or
beginBackgroundTaskWithExpirationHandler: method generates a unique
token to associate with the corresponding task. When your app
completes a task, it must call the endBackgroundTask: method with the
corresponding token to let the system know that the task is complete.
Failure to call the endBackgroundTask: method for a background task
will result in the termination of your app.

iOS: how to detect when the UIApplicationDelegate state becomes "suspended"?

How can we detect when an iOS App has been suspended?
There is no method that mentions this in the official UIApplicationDelegate documentation.
These are the states that an App can have:
(source: apple.com)
Use case:
I want to log when an app stops running subsequently to being woken up due a location event. For example I have got an iBeacon that the app is montioring. I activate the iBeacon and the app gets launched successfuly in background (for 10 seconds). I would like to detect when the App stops running after these 10 seconds have elapsed. However there is no AppDelegate method that seem to allow to intercept this (please consider that I am investigate this specific case.
Previous question:
I had asked a previous similar question which did not get answered. Please find it here.
While I am unaware of any callback, you can query for the amount of background time remaining with:
NSLog(#"background time remaining: %8.2f", [UIApplication sharedApplication].backgroundTimeRemaining);
Theoretically, you can put this in a thread and execute custom code a second or so before your app terminates. See my blog post here for more info:
http://developer.radiusnetworks.com/2014/11/13/extending-background-ranging-on-ios.html
I think you won't get any feedback form Suspended state. Suspended means that app is in memory but no code is executing right now.
Documentation:
The app is in memory but is not executing code. The system suspends apps that are in the background and do not have any pending tasks to complete. The system may purge suspended apps at any time without waking them up to make room for other apps.
So in my understanding, if an app would give you a callback with something like applicationDidEnterSuspendedState it will be a paradox, cause Suspended state means that no code is executed.

Running an application in the Background?

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.

Resources