我在我的应用程序中有一个后台过程。下面是代码:
@available(iOS 10.0, *)
func startTimer() {
timer2?.invalidate()
timer2 = Timer.scheduledTimer(withTimeInterval: 0.5, repeats: true, block: { (t) in
if UIApplication.shared.applicationState == .background {
NSLog("tic background \(UIApplication.shared.backgroundTimeRemaining)")
if UIApplication.shared.backgroundTimeRemaining < 10 {
}
}
})
timer2?.fire()
}但是,它只在iOS10中工作。有办法重写以前版本的iOS吗?
发布于 2017-01-08 09:54:58
在ios9或更低版本上没有基于块的计时器API。
要么使用方法作为回调,而不是块,要么使用第三方解决方案,例如BlocksKit (有数百个)
Id使用一种方法:
func startTimer() {
timer2?.invalidate()
timer2 = Timer.scheduledTimer(timeInterval: 0.5, target: self, selector: #selector(ViewController.timerFired(_:)), userInfo: nil, repeats: true)
timer2?.fire()
}
@objc
func timerFired(_ timer: Timer) {
NSLog("tic")
if UIApplication.shared.applicationState == .background {
NSLog("tic background \(UIApplication.shared.backgroundTimeRemaining)")
if UIApplication.shared.backgroundTimeRemaining < 10 {
}
}
}https://stackoverflow.com/questions/41531140
复制相似问题