以Swift为背景的计时器
我想做以Swift为背景的计时器,swift,timer,Swift,Timer,我想做Timer应用程序,启动活动和按下Home Timer将继续工作需要一段时间,但当我使用下面的代码时,它在前台正常工作,但当我按下Home和它在后台工作时,秒数大于60,在这种情况下,它会给我奇怪的数字,如00:01:75。你知道如何解决这个错误吗?提前谢谢 override func viewDidLoad() { super.viewDidLoad() NotificationCenter.default.addObserver(self, selector: #select
Timer
应用程序,启动活动和按下Home Timer将继续工作需要一段时间,但当我使用下面的代码时,它在前台正常工作,但当我按下Home和它在后台工作时,秒数大于60,在这种情况下,它会给我奇怪的数字,如00:01:75。你知道如何解决这个错误吗?提前谢谢
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(pauseWhenBackground(noti:)), name: UIApplication.didEnterBackgroundNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(willEnterForeground(noti:)), name: UIApplication.willEnterForegroundNotification, object: nil)
}
@objc func willEnterForeground(noti: Notification) {
if let savedDate = UserDefaults.standard.object(forKey: "savedTime") as? Date {
(diffHrs, diffMins, diffSecs) = AddWorkTime.getTimeDifference(startDate: savedDate)
self.refresh(hours: diffHrs, mins: diffMins, secs: diffSecs)
}
}
static func getTimeDifference(startDate: Date) -> (Int, Int, Int) {
let calendar = Calendar.current
let components = calendar.dateComponents([.hour, .minute, .second], from: startDate, to: Date())
return(components.hour!, components.minute!, components.second!)
}
func refresh (hours: Int, mins: Int, secs: Int) {
self.hrs += hours
self.min += mins
self.sec += secs
self.time.text = String(format: "%02d : %02d : %02d", self.hrs, self.min, self.sec)
self.timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: (#selector(AddWorkTime.updateLabels(t:))), userInfo: nil, repeats: true)
}
@objc func updateLabels(t: Timer) {
if (self.sec == 59) {
self.min += 1
self.sec = 0
}
if (self.min == 60) {
self.hrs += 1
self.min = 0
}else{
self.sec += 1
}
self.time.text = String(format: "%02d : %02d : %02d", self.hrs, self.min, self.sec)
}
@objc func pauseWhenBackground(noti: Notification) {
self.timer.invalidate()
let shared = UserDefaults.standard
shared.set(Date(), forKey: "savedTime")
}
func resetContent() {
self.removeSavedDate()
timer.invalidate()
self.time.text = "00 : 00 : 00 "
self.sec = 0
self.min = 0
self.hrs = 0
}
func removeSavedDate() {
if (UserDefaults.standard.object(forKey: "savedTime") as? Date) != nil {
UserDefaults.standard.removeObject(forKey: "savedTime")
}
}
确保打开背景模式并在xCode中设置所需的值。这很奇怪,但即使关闭了背景模式,此代码也可以工作。在AppDelegate中设置
application.beginBackgroundTask{}
之后,任何计时器似乎都无法工作
我使用以下代码:
在AppDelegate中,添加以下代码:
func applicationDidEnterBackground(_ application: UIApplication) {
application.beginBackgroundTask {} // allows to run background tasks
}
例如,在需要的位置调用下面的方法或检查计时器
func registerBackgroundTask() {
DispatchQueue.global(qos: .background).asyncAfter(deadline: DispatchTime.now() + 5, qos: .background) {
print("fasdf")
}
}
我在ViewDidLoad中禁用了pauseWhenBackground和willEnterForeground,在ViewDidLoad中添加了registerBackgroundTask(),并启用了后台模式,它可以正常工作。谢谢。我收到这个消息:由于信号9和应用程序关闭而终止。这可能取决于你在哪个线程中调用什么。。。没有任何背景,我一点也不知道\