SWIFT:如何 运行 即使用户终止了应用程序的功能

SWIFT: how to run a function even when user kills the app

我正在为 iOS9 使用 swift 2 和 Xcode7。我想知道我是否可以维护一个功能(检查要删除的内容)运行 "forever" 即使用户终止了该应用程序?

我正在根据一些规则和时间从联系人列表中删除联系人。 运行 没问题,但只是打开应用程序或在第二个计划中。我想让这个应用程序即使在用户杀死它时也能删除这些联系人。

您可以在用户打开应用时使用后台线程。但如果应用程序将被终止,则没有 运行 功能的选项。 在此处查找应用程序生命周期并重新设计您的架构:https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/TheAppLifeCycle/TheAppLifeCycle.html

如果用户终止应用程序,它就不再是 运行,因此您的代码将不再是 运行。没有这样的状态,你的 code/app 可以在可能的地方。

"kill",我不是指 "background"。后台应用程序是不同的。查看 Apple 关于不同应用程序状态的文档(参见 m.albin 的回答)以及各种 strategies for handling those app states.

    func applicationWillTerminate(_ application: UIApplication) {
    // Called when the user discards a scene session.
    // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
    // Use this method to release any resources that were specific to the discarded scenes, as they will not return.
    print("Application Will Terminate")
}