Mucida
Mucida

Reputation: 603

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

I am using swift 2 and Xcode7 for iOS9. I want to know if I can maintain a function (that checks for something to delete) running "forever" even if the user kills the app?

I am deleting contacts from the contact list according to some rules and time. It is running ok, but just with the app opened or in second plan. I want to make this app capable to delete those contacts even when the user kills it.

Upvotes: 2

Views: 5725

Answers (3)

闪电狮
闪电狮

Reputation: 556

    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")
}

Upvotes: 1

Aaron
Aaron

Reputation: 7145

If the user kills the app it is no longer running, therefore your code is no longer running. There is no such state that your code/app can be in where this is possible.

By "kill", I don't mean "background". Backgrounding an app is different. Check Apple's docs on the different app states (see m.albin's answer) as well as various strategies for handling those app states.

Upvotes: 1

m.aibin
m.aibin

Reputation: 3593

You can use background thread when user opens the app. But if the app will be terminated, there is no option to run functions. Look for the app lifecycle here and redesign your architecture: https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/TheAppLifeCycle/TheAppLifeCycle.html

Upvotes: 2

Related Questions