SWIFT: how to run a function even when user kills

2019-07-20 16:55发布

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.

2条回答
我只想做你的唯一
2楼-- · 2019-07-20 17:47

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.

查看更多
欢心
3楼-- · 2019-07-20 17:51

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

查看更多
登录 后发表回答