简体   繁体   中英

Swift IOS launch notification when app terminated

I would like to send a notification when my app is terminated. Is this possible using background processes to send a message as the app is killed? I'm using swift.

Thanks, Rob.

No. If your app is backgrounded, it is suspended. It is not running. If it is now terminated, it doesn't get brought back to a running state and sent a message of some sort. It just dies silently in its sleep.

you can use this function in your AppDelegate

func applicationWillTerminate(_ application: UIApplication) {
        bgTask = application.beginBackgroundTask(withName:"appWillEnd", expirationHandler: {() -> Void in
            // Do something to stop our background task or the app will be killed

        })

        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }

I hope that you can use it

The best you can reasonably hope for is to have the OS tell you when the user is leaving the app. Eg you can implement applicationDidEnterBackground(_:) or applicationWillResignActive(_:) (or if using iOS 13 scene delegate, implement sceneDidEnterBackground(_:) or sceneWillResignActive(_:) ).

For more information, see Preparing Your UI to Run in the Background or About the Background Execution Sequence .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM