【问题标题】:How to make an iOS app execute some code even when app is not running in the background?即使应用程序不在后台运行,如何让 iOS 应用程序执行一些代码?
【发布时间】:2017-02-15 05:46:08
【问题描述】:

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

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

【问题讨论】:

  • Apple 不提供这样的功能,您只能在应用程序为后台时调用服务。当您的应用程序未运行或终止时,您无法执行任何操作来运行服务
  • 你确定。没有任何替代品。可能是一些第三方代码。
  • 我确信当您的应用程序未运行或终止时,不会有任何东西可以帮助您运行服务。看我的问题和网友stackoverflow.com/questions/39720213/…

标签: ios swift nsnotificationcenter background-fetch


【解决方案1】:

当应用程序在 iOS 中终止时,您无法运行任何代码,如 documentation 中给出的。

您可以通过以下方式在应用程序终止之前做一些事情。

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

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-05-27
    • 1970-01-01
    • 1970-01-01
    • 2015-01-04
    • 1970-01-01
    • 2020-08-31
    • 2013-02-26
    相关资源
    最近更新 更多