Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I simulate app being killed in background?

I'm trying to verify that my app (App1) behaves correctly when it is shut down by the system after it launches another app (App2). Is there any way to simulate or force this behavior?

Telling the simulator to simulate a memory warning while App2 is running doesn't do anything until App1 is brought back to the foreground. Would killing App1 from the debugger simulate the same sequence of app events?

EDIT: My app is placed in the background when it launches a second app to handle a file. This second app then relaunches my app, and I'm trying to verify that my app behaves correctly when the system has shut it down while the second app is executing.

like image 348
Greg Avatar asked Feb 19 '13 23:02

Greg


People also ask

Does iOS kill app in background?

If limits are exceeded by an app with background processing, for a certain time, iOS will kill the app. BGProcessingTask gives an unrestricted few minutes to your app to run processing tasks in the background, while the device charges overnight.

How do you kill an app in iOS simulator?

First press Shift+Command+ H twice. This will open up all the apps that are currently open. Swipe left/right to the app you actually want to close. Just Swipe Up using the Touchpad while Holding the App would close the app.

Will iOS terminate the app running in background after a specific time?

At the same time, didReceiveMemoryWarning is invoked for the app. At this point, so that your app continues to run properly, the OS begins terminating apps in the background to free some memory. Once all background apps are terminated, if your app still needs more memory, the OS terminates your app.


2 Answers

iOS 14.5 and Later

Enable Settings - Developer - Fast App Termination on the device:

screenshot of Settings - Developer - Fast App Termination

This will terminate, instead of suspend, your app when they're backgrounded. This is useful primarily for testing state restoration logic.


iOS 14.4 and Earlier

As the other answers note, you can immediately end your app by force-quitting it or by stopping the debugger. Your app will get no warning in those cases.

If you're looking to test your applicationWillTerminate methods, set UIApplicationExitsOnSuspend to YES in your .plist and then switch apps or press the home button. Make sure you set the key to a Boolean, not the string "YES".

like image 84
Aaron Brager Avatar answered Sep 25 '22 06:09

Aaron Brager


Yes, if your app is in the background already. An app killed by the system while in the background is terminated with no warning-- no app delegate methods are called, no state changes are made-- which is exactly what happens when you do a debugger stop while the app is in the background. (This presupposes that you've already put your app in the background by clicking the home button on the simulator or Cmd-Shift-H)

(As @Inafziger notes, you can also get the same effect by using the simulator's interface to force quit the running app.)

like image 30
Ben Zotto Avatar answered Sep 23 '22 06:09

Ben Zotto