问题
I'm making an alarm clock-like app. The user should be notified with a vibration and a sound every 10-60 seconds over a period of about 30 minutes. What is the best approach for this? It's easy enough doing it in the foreground, but how can i continue to run scheduled code in the background? I could use local notifications, but the user doesn't receive them when in "do not disturb" mode.
回答1:
You should review the documentation about background execution: Background Execution and Multitasking. There are multiple ways of consistently running code in the background, but your app has to meet certain guidelines to use them--in your case, I don't think your app neatly falls into any of the categories Apple describes in Implementing Long-Running Background Tasks. There's always the possibility the submission reviews could approve your app anyway.
To avoid potential app rejection, you should implement your app using local notifications, and include a warning in your app that users should disable "Do Not Disturb" for it to function correctly. Unfortunately, there isn't a way for users to exclude apps from DND, nor is there a public API to manipulate DND.
来源:https://stackoverflow.com/questions/17181130/running-code-repeatedly-in-background-on-ios