问题
I want to invoke a method daily at a specific time in my iPhone application. How can I achieve this?
回答1:
There's not a whole lot you can do with the iPhone SDK and a closed app. You can have it send local push notifications at specific times, but that won't execute your code until the user manually opens your app.
See this related question recently asked: Is it possible to have my closed app run code in iOS?
One thing I've seen apps do (like alarm clock apps, etc) is have you leave your app open and just let the phone fall asleep and then it will still execute code when you want it to. But that only works as long as the user doesn't hit the home button.
回答2:
Schedule a local notification. This is how most basic alarm apps work. The app needs to be running sometime before the method execution time to schedule a local notification. After the local notification has been scheduled, the app can be closed and the method will be invoked at the specific time.
If you have push notifications configured, then your app does not need to run at all to schedule the method. It can all be done from another application.
Check out this documentation on local and push notifications from Apple.
回答3:
you cannot run a specific code inside your application when its not running ..iOS allow only limited functionality to be run in background or when app is quit.
来源:https://stackoverflow.com/questions/8647731/how-to-invoke-a-method-daily-at-a-specific-time-in-iphone-application