Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to execute code when my app terminated in background in ios

Tags:

ios

background

In iOS, we all know that there is AppDelegate method applicationWillTerminate, and it is called when my app is closed by user when it is currently running(i.e. not in background). But I want to do something(save data, for example) when my app is terminated(closed by user or killed by OS) when it runs in background.

PS: my app can run in background.

Do you have any solutions? thanks.

like image 387
T.Zach Avatar asked Aug 26 '15 09:08

T.Zach


People also ask

How can we execute code when app is not running in iOS?

You can't run any code when the app is terminated in iOS as given in the documentation. You can do something before the app is going to get terminated by using the following ways. func applicationWillTerminate(_ application: UIApplication) { // Called when the application is about to terminate.

How do I force an app to run in the background iOS?

For iOS Devices If Background refresh is greyed out in the ON position, go To Settings App - > General - > Background App Refresh - > Turn on the option for the system, and then you can turn on / off by app.

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

iOS places strict limits on how long a background task can run, and if the EndBackgroundTask call is not made within the allotted time, the application will be terminated. By keeping track of the remaining backgrounding time, and using expiration handlers when necessary, we can avoid iOS terminating the application.

Can I run an app in the background iOS?

Some apps keep running in the background when you return to the home screen. You can set your mobile phone to refresh apps in the background so that you'll still get notifications even if the app isn't actively in use.


2 Answers

If you need to execute code when your app isn’t running, there are several options open to you depending on what you’re trying to do.

  1. Background fetch will let your app run in the background for about 30 seconds at scheduled intervals. The goal of this is to fetch data and prepare your UI for when the app runs next.
  2. Push notifications let your app fetch fresh data from your server. You can make a message appear on the device if you want, but it’s not required – silent push notifications let you skip that part.
  3. Local notifications let you display an alert to the user, along with any media attachments you want and some options for the user to select from. If they choose those options then your app can be launched in the foreground or background to handle them.

From: https://www.hackingwithswift.com/example-code/system/how-to-run-code-when-your-app-is-terminated

You can also use Silent Push Notification as I mentioned in a similar question: https://stackoverflow.com/a/57245980/6157415

like image 145
Mike D3ViD Tyson Avatar answered Nov 04 '22 02:11

Mike D3ViD Tyson


Sorry but you should use applicationWillTerminate:

This method lets your app know that it is about to be terminated and purged from memory entirely. You should use this method to perform any final clean-up tasks for your app, such as freeing shared resources, saving user data, and invalidating timers. Your implementation of this method has approximately five seconds to perform any tasks and return. If the method does not return before time expires, the system may kill the process altogether.

For apps that do not support background execution or are linked against iOS 3.x or earlier, this method is always called when the user quits the app. 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. However, this method may be called in situations where the app is running in the background (not suspended) and the system needs to terminate it for some reason.

So if you need to save data ALSO when user manually kill the app use applicationDidEnterBackground that it's called if your app support background mode.

like image 31
Massimo Polimeni Avatar answered Nov 04 '22 03:11

Massimo Polimeni