0

在 Android 上,服务让我们可以灵活地继续运行代码,即使应用程序没有在后台运行,因为即使启动服务的应用程序终止,服务也会继续执行。

我希望在 iOS 上做同样的事情。我知道backgroundFetch,但它仅在应用程序在后台运行时才有效。

4

1 回答 1

1

当应用程序在 iOS 中终止时,您无法运行任何代码,如文档中所述。

您可以使用以下方式在应用程序终止之前执行某些操作。

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

}

或通过在应用程序的任何地方注册通知

NotificationCenter.default.addObserver(forName: NSNotification.Name.UIApplicationWillTerminate, object: nil, queue: OperationQueue.main) { (notification) in

}
于 2017-02-15T06:48:58.833 回答