使计时器在后台 iOS 上运行 3 分钟以上 [英] make timer run on background iOS for more than 3 minutes

查看:17
本文介绍了使计时器在后台 iOS 上运行 3 分钟以上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我需要这个应用程序运行超过 3 分钟的计时器,并播放大约 45 秒的声音,这里的大多数解决方案在 iOS 上只运行不到 3 分钟.有没有办法让它一直运行,除非应用程序停止?

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?

经过研究,我想解决方案是实现 beginBackgroundTaskWithExpirationHandler

After the research, I guess the solution is implement beginBackgroundTaskWithExpirationHandler

并通过提供 handler 并在 plist 上设置位置更新来停止它?

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

音频方面,除了设置plist,播放音频还需要做什么?

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

​​Swift如何使用NSTimer后台?iphone - NSTimers 在后台​​在后台运行应用程序超过 10 分钟

推荐答案

不,为了让我们所有人免于消耗我们电池的应用程序,Apple 不允许应用程序继续在后台运行超过几个分钟.只有非常特殊的限制(音乐播放应用程序、VOIP、导航应用程序等)允许持续操作.关于细节,这在关于后台执行序列.

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 About the Background Execution Sequence.

如果您想在未来某个时间通知用户某事,您可以使用本地通知.但是,当您这样做时,您无法控制您的应用程序是否重新启动.只有当用户点击通知时(假设他们甚至授予您的应用程序通知权限),应用程序才会重新打开.有关详细信息,请参阅 UserNotification 框架文档中的本地通知讨论.但请注意,这并不是针对每 45 秒发出一次警报,而是针对安排在未来某个时间的重要通知.

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 UserNotification framework documentation. But note, this is not intended for alerts every 45 seconds, but rather for a significant notification scheduled for some future time.

有关如何将本地通知与计时器结合的讨论,请参阅​​swift NSTimer in Background这个后续问题快速计算在后台运行的计时器的时间.

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.

这篇关于使计时器在后台 iOS 上运行 3 分钟以上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆