[英]Issues regarding to Timer running in the background iOS 10
我正在制作一个可以帮助人们追踪工作间隔的应用程序。
我需要的是,无论应用程序处于前台还是后台,计时器都应至少运行30分钟。
func startFocus() {
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(Pomodoro.focusIntervalCounter), userInfo: nil, repeats: true)
}
func focusIntervalCounter() {
dynamic_focusIntervalSecond -= 1
focusDelegate?.updatingFocusCountingDown(timeStamp: seconds2Timestamp(intSeconds: dynamic_focusIntervalSecond), secondLeft: dynamic_focusIntervalSecond)
if dynamic_focusIntervalSecond == 0 {
timer.invalidate()
focusDelegate?.focusCountingDowndid(end: true)
}
}
focusIntervalCounter() should invalid the timer when the `dynamic_focusIntervalSecond` is 0.
当程序在前台时,它可以正常工作,但是在关闭屏幕后,计时器会工作一会儿并停止。 这是继续计时器计数的任何方法吗?
提前致谢。
否,您最多可以运行5分钟的后台任务,使用后台更新功能来触发应用刷新,或使用通知来触发后台操作。 iOS中无法保证30分钟后任何代码都会在后台连续运行。 用户在通知中选择一个操作后,本地通知将使您的代码得以运行。 静默的推送通知可以在后台打开的收据中运行某些代码,但是需要互联网连接,并且不能保证将其传递。
有关更多信息,请参见此问题: 后台的iOS计时器
不,您不能在后台模式下运行计时器。 您可以创建一个UIBackgroundTaskIdentifier
,正如我观察到的UIBackgroundTaskIdentifier
,它将为您提供180秒的时间,我不确定它是否会随操作系统版本而变化。
您可以尝试安排30分钟的本地通知。
如果您使用推送通知,Airplay,位置,VOIP应用程序,蓝牙,报亭,背景获取,则可以启用背景模式,有关更多详细信息,请阅读此Apple开发者文档BackgroundExecution
有些事情在后台是不可能的,您是否切换了项目以启用后台模式? 你可以在这里更多地了解它
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.