如何检测应用程序是否会被杀死?

时间:2017-12-14 11:49:04

标签: ios swift

我需要在应用程序被杀死时向服务器发送消息(通过用户退出应用程序或系统终止应用程序)。我怎么能这样做?

2 个答案:

答案 0 :(得分:2)

此处理代码

 func applicationWillTerminate(_ application: UIApplication) {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.

  NotificationCenter.default.post(name:Notification.Name("appwillTerminate"), object:"")


}

答案 1 :(得分:2)

你可以像这样使用NotificationCenter来处理 NSNotification.Name.UIApplicationWillTerminate see here

NotificationCenter.default.addObserver(self, selector: #selector(test), name: NSNotification.Name.UIApplicationWillTerminate, object: nil)