iOS: force-closed app restarting in background - ios

We're seeing a strange situation on our iOS app that is impacting our user experience.
When a user background's the app (by pressing the Home button), we can clearly see the app entering the background. When the user then force-closes the app (by double-tappinging the Home button and sliding-up on the app), the app terminates normally. When the app is then re-opened, it follows the normal iOS start-up sequencing.
However, if the user double-taps the Home button while the app is running (resigning the app from active, but not sending it to background) and then force-closes the app (by sliding-up on the app), we're seeing the app terminate (the process gets killed), but then the app immediately re-starts in the background (with a new pid). The app is not designed to start-up or run in the background, and this is causing UX issues.
The app has no entitlements to run in the background, although we do have a couple of third-party libraries that have tasks that wind-down when the app enters the background.
I suspect the issue is a result of the app being force-closed from a non-active state rather than a background-state. If the app enters the background first, the third-party tasks are getting a chance to complete. If the app is force-closed from the non-active state, the tasks never get a change to complete in the background, so iOS is restarting the app in background to give those tasks a change to finish. It's unclear, however, if this is correct behavior.
-Stix

I'm kinda thinking the questions were self-evident, but if not:
Is this correct behavior for iOS? Should iOS be restarting the app in the background when the user force-closes the app w/o putting it into background?
If this is correct behavior, what can cause this to occur? Is it possible some third-party framework - still running a background task that hasn't completed - is the culprit?
The force-close works fine if the app is put into background first (giving background tasks the ability to complete/expire). Is there anyway to force it into background when force-closing the app while it's in a non-active state?

Related

Detect when app killed by user in background state iOS

I am working in a chat application where I need to show user status (offline/online).
When my app is in foreground and background then I need to show user as online (managing by VoIP).
But when the user kill the app then it should go to offline.
I have to maintain a flag to show offline which I am managing in delegate function applicationWillTerminate but this function only called when app is in foreground state and user kill it by pressing double tap home button and swipe up.
This function does not get called when app is in background state. I mean simply press home by single tap (app will go in background) then again double tap to swipe up.
Is there any function where I get 100% call either app is in background/foreground state and user kill the app?
Is there any function where I get 100% call either app is in background/foreground state and user kill the app?
No. Just the opposite. If your app is terminated when already in the background, if it is suspended (ie not running in the background due to special entitlement), it is 100% certain you will get no event. You cannot. You are suspended and not running. The app dies in its sleep.
No, As per Apple Document
https://developer.apple.com/documentation/uikit/uiapplicationdelegate/1623111-applicationwillterminate?language=objc
For apps that support background execution, this method is generally not called when the user quits the app because the app simply moves to the background in that case.
What you can do execute a method(which hit an API for keeping status online) after few seconds(whatever you find suitable time) when you app goes in Background, If method is calling successfully after that specific seconds then user stay online, if its not call after specified second then server update its status to offline. So It require both server and client handling.

Questions about app state after tapping on app icon after apps enters suspended state

I’m putting an app into background.
Assuming that I’m not doing anything to keep the app alive in the background, then the app goes through suspended state in a matter of 5 seconds. Right?
What happens if I then tap on the app icon? That’s not suppose to trigger a didFinishLaunch right? It will just bring me back to the last screen I was at and also trigger didbecomeActive & willenterforeground notifications. I won't be getting any other callback. Right?
Assuming there is no restart of the phone, point 2 is true even if there are hours between me tapping home and then tapping back the app icon. Right? Does it also persist device restarts but not force-restart?
The only time I won’t be brought back to the screen I was at (before hitting home) is if the device receives a memory warning and my app is flushed out of suspended state. At this point tapping on the app icon will result in didFinishLaunch. Right?
(I’m asking all of this because sometimes after putting the app in the background and tapping the app icon again (e.g. 10 minutes later), the app is going through it’s launch phase. Most of the time it just goes back to its previous screen)
I've already seen Will ios terminate the app running in background after a specific time? but that doesn't address all aspects I want.
It will just bring me back to the last screen I was at and also trigger didbecomeActive & willenterforeground notifications.
Right, if your app was not terminated in the background.
I won't be getting any other callback.
Not necessarily true. If you were summoned to the front by a local notification, for example, you'll also get an event about that.
Assuming there is no restart of the phone, point 2 is true even if there are hours between me tapping home and then tapping back the app icon.
Not necessarily. The app might well be terminated silently in the background.
Does it also persist device restarts but not force-restart?
Absolutely not. How can the app run when the device is off? Shutting down the app terminates every app.
I’m asking all of this because sometimes after putting the app in the background and tapping the app icon again (e.g. 10 minutes later), the app is going through it’s launch phase
It's not a matter of time. The watchdog process is constantly combing the suspended apps looking for the ones that take up too much memory so that other apps can run. You must not be surprised if yours is one of them.
You can come back to the front launching from scratch or by coming back to life from suspension; it's the most basic fact of iOS app life! You just need to accept it.
But there are lots of things you can do to reduce your chances of being terminated in the background. Giving up memory-consuming objects as you background is first on the list.

Is there anyway to check when an app will be suspended?

Is there a universal time of iOS app suspension time (i.e when it goes out of background mode and terminates).
Background
The app is in the background and executing code. Most apps enter this
state briefly on their way to being suspended. However, an app that
requests extra execution time may remain in this state for a period of
time. In addition, an app being launched directly into the background
enters this state instead of the inactive state. For information about
how to execute code while in the background, see Background Execution.
Suspended
The app is in the background but is not executing code. The system
moves apps to this state automatically and does not notify them before
doing so. While suspended, an app remains in memory but does not
execute any code. When a low-memory condition occurs, the system may
purge suspended apps without notice to make more space for the
foreground app.
No, there is not any possibility to know about that, according to this:
Suspended: ...The system moves apps to this state automatically and does not notify them before doing so...
Link: https://developer.apple.com/library/content/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/TheAppLifeCycle/TheAppLifeCycle.html

Determine the app state on pressing home button twice with app running

I would like to know as to what the application state would be on hitting the iphone "Home" button twice with the application running.
The scenario is something like below:
My iOS app is running on the foreground
With the application running hit the home button twice to bring up the multi-tasking taskbar (obviously my app is not listed here because it's not a recently used app and is still running in the foreground)
Now press anywhere outside the taskbar (i.e in the application) and app will be back in focus again
My questions:
What state would be app be on performing above step #2? Would it enter background or still in foreground? What method would get triggered here?
On performing step #3, would the app re-enter foreground from background? Again what method would get triggered here?
Any hints/suggestions would be very helpful.
The application is about to move from active to inactive state, so it's still in-between. You should be able to use -applicationWillResignActive: in your UIApplicationDelegate.
After returning to the application, the application becomes active again thus receives a -applicationDidBecomeActive: on your UIApplicationDelegate.

When an iOS app directly enters the background state?

Can someone tell me a scenario when an iOS application directly enters the background state?.
Here I have quoted the lines from iOS Application Programming Document in multitasking section.
If your app is launched into the background instead—usually to handle
some type of background event—the launch cycle changes slightly to the
one shown in Figure 3-3. The main difference is that instead of your
app being made active, it enters the background state to handle the
event and then is suspended shortly afterward.
Added ...
In the iOS Application Programming Document if you see the figure 3.3 titled Launching an app into the background, the flow is like this User taps app icon -> main() -> UIApplicationMain() -> Enter background. Is there any chance when the app directly enters background when an user taps app icon. I interpreted the image like this. Is it correct?
Thanks.
One scenario for a background launch (App X)
X registered for location background mode in its Info.plist
X is run by the user, and registers for significant location changes while running
The user switches to another app Y, so X goes to background and is then suspended (it will be returned to background mode whenever there is a significant location change to handle, and then be suspended again)
The app Y eats lots of memory, so suspended applications (including X) get kicked out of memory
a significant location change comes in. Now X is launched into background.
Scenario
Lets say you had registered your application for Local/Push notification. Then your application will launch in background run some code which have written inside your applicationDidEnterBackgroud: delegate method and then it terminates immediately.
Check listing 2
Apple documentation
EDIT:
Applications might also find local notifications useful when they run
in the background and some message, data, or other item arrives that
might be of interest to the user. In this case, they should present
the notification immediately using the UIApplication method
presentLocalNotificationNow: (iOS gives an application a limited time
to run in the background). Listing 2-2 illustrates how you might do
this.

Resources