简体   繁体   English

应用程序终止时的 Swift IOS 启动通知

[英]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你可以在你的 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(_:) ).例如,您可以实现applicationDidEnterBackground(_:)applicationWillResignActive(_:) (或者如果使用 iOS 13 场景委托,则实现sceneDidEnterBackground(_:)sceneWillResignActive(_:) )。

For more information, see Preparing Your UI to Run in the Background or About the Background Execution Sequence .有关更多信息,请参阅准备 UI 以在后台运行关于后台执行序列

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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