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