make timer run on background iOS for more than 3 m

2019-01-20 06:26发布

So I need this app to run timer for more than 3 minutes, and play a sound like very 45 seconds, most of the solutions here are just for less than 3 minutes on iOS. Is there a way to make it run all the time, unless the app stops?

After the research, I guess the solution is implement beginBackgroundTaskWithExpirationHandler

and stops it by giving a handler and set location update on plist?

As for audio, besides setting the plist, anything else need to do to play audio?

Swift how to use NSTimer background? iphone - NSTimers in background Run app for more than 10 minutes in background

1条回答
ゆ 、 Hurt°
2楼-- · 2019-01-20 07:04

No, in order to save all of us from apps that kill our batteries, Apple does not allow apps to continue to run in the background for more than a few minutes. There are only very special limitations (music playing apps, VOIP, navigation apps, etc.) which permit ongoing operation. In terms of details, this is described in Background Execution chapter of App Programming Guide for iOS.

If you want to notify user of something at some future time, you can use local notifications. When you do this, though, you don't control whether your app restarts or not. Only if the user taps on the notification (assuming they even granted your app notification privileges at all), will the app be reopened. For more information, refer the the local notification discussions in Local and Remote Notification Programming Guide. But note, this is not intended for alerts every 45 seconds, but rather for a significant notification scheduled for some future time.

For discussion of how one might marry local notifications with timers, see swift NSTimer in Background and this follow up question swift calculate time for timers running in background.

查看更多
登录 后发表回答