手机睡眠时运行定时器

问题描述 投票:1回答:1

我正在构建一个应用程序,我需要一个计时器来运行,如果用户将屏幕发送到后台,或者如果他们将手机置于睡眠状态并再次打开它。我需要定时器仍然在运行。

我试着记录我退出和再次进入时的时间,减去两者并将其添加到运行计数中,它似乎在Xcode模拟器上工作得很好,但当我在手机上运行它时,它不工作。有什么好办法吗?

下面是代码,供参考。而定时器的开始是一个按钮,我没有包括这部分,但它只是一个简单的IBAction,调用定时器.fire()函数。

var time = 0.0
var timer = Timer()
var exitTime : Double = 0
var resumeTime : Double = 0

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(true)
    exitTime = Date().timeIntervalSinceNow
}

override func awakeFromNib() {
    super.awakeFromNib()
    resumeTime = Date().timeIntervalSinceNow
    time += (resumeTime-exitTime)
    timer.fire()
}


func startTimer() {
    if !isTimeRunning {
        timer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: 
        #selector(WorkoutStartedViewController.action), userInfo: nil, repeats: true)
        isTimeRunning = true
    }
}

func pauseTimer() {
    timer.invalidate()
    isTimeRunning = false
}

@objc func action()
{
    time += 0.1
    timerLabel.text = String(time)
    let floorCounter = Int(floor(time))
    let hour = floorCounter/3600
    let minute = (floorCounter % 3600)/60
    var minuteString = "\(minute)"
    if minute < 10 {
        minuteString = "0\(minute)"
    }

    let second = (floorCounter % 3600) % 60
    var secondString = "\(second)"
    if second < 10 {
        secondString = "0\(second)"
    }

    if time < 3600.0 {
        timerLabel.text = "\(minuteString):\(secondString)"
    } else {
        timerLabel.text = "\(hour):\(minuteString):\(secondString)"
    }

}
ios swift xcode timer sleep
1个回答
1
投票

你的想法是对的,但我看到的第一个问题是 viewWillDissapear 只有当你离开视图控制器进入新的视图控制器时才会被调用--当应用程序离开视图进入后台(按下主按钮)时不会被调用。

我相信你要找的回调函数是 UIApplication.willResignActive (去背景)和 UIApplication.didBecomeActive (应用程序重新开放)

您可以在 AppDelegate 或者你可以将它们设置在视图控制器上,这里是你的代码和一些修改的混合,以在一个初始VC上产生一个工作样本。

import UIKit
import CoreData

class ViewController: UIViewController {

    @IBOutlet weak var timerLabel: UILabel!

    var time = 0.0
    var timer = Timer()
    var exitTime : Date?    // Change to Date
    var resumeTime : Date?    // Change to Date
    var isTimeRunning = false

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        startTimer()
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        NotificationCenter.default.addObserver(self,
        selector: #selector(applicationDidBecomeActive),
        name: UIApplication.didBecomeActiveNotification,
        object: nil)
        // Add willResign observer
        NotificationCenter.default.addObserver(self,
        selector: #selector(applicationWillResign),
        name: UIApplication.willResignActiveNotification,
        object: nil)
    }

    override func viewWillDisappear(_ animated: Bool) {
        // Remove becomeActive observer
        NotificationCenter.default.removeObserver(self,
                                                  name: UIApplication.didBecomeActiveNotification,
                                                  object: nil)
        // Remove becomeActive observer
        NotificationCenter.default.removeObserver(self,
                                                  name: UIApplication.willResignActiveNotification,
                                                  object: nil)

    }

    func startTimer() {
        if !isTimeRunning {
            timer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector:
                #selector(self.action), userInfo: nil, repeats: true)
            isTimeRunning = true
        }
    }

    @objc func action() {
        time += 0.1
        timerLabel.text = String(time)
        let floorCounter = Int(floor(time))
        let hour = floorCounter/3600
        let minute = (floorCounter % 3600)/60
        var minuteString = "\(minute)"
        if minute < 10 {
            minuteString = "0\(minute)"
        }

        let second = (floorCounter % 3600) % 60
        var secondString = "\(second)"
        if second < 10 {
            secondString = "0\(second)"
        }

        if time < 3600.0 {
            timerLabel.text = "\(minuteString):\(secondString)"
        } else {
            timerLabel.text = "\(hour):\(minuteString):\(secondString)"
        }
    }

    @objc func applicationDidBecomeActive() {
        // handle event
        lookForActiveTimers()
    }

    func lookForActiveTimers() {

        var timers = [NSManagedObject]()

        guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else {
            return
        }
        let managedContext = appDelegate.persistentContainer.viewContext
        let fetchRequest = NSFetchRequest<NSManagedObject>(entityName: "Timers")

        //3
        do {
            timers = try managedContext.fetch(fetchRequest)
            print("timers: \(timers)")

            var activeTimer: NSManagedObject?

            for timer in timers {
                if let active = timer.value(forKey: "active") as? Bool {
                    if active {
                        activeTimer = timer
                    }
                }
            }

            if let activeTimer = activeTimer {

                // Handle active timer (may need to go to a new view)
                if let closeDate = activeTimer.value(forKey: "appCloseTime") as? Date {

                    if let alreadyTimed = activeTimer.value(forKey: "alreadyTimed") as? Double {

                        let now = Date()
                        let difference = now.timeIntervalSince(closeDate)

                        // Handle set up again here
                        print("App opened with a difference of \(difference) and already ran for a total of \(alreadyTimed) seconds before close")

                        time = alreadyTimed + difference
                        startTimer()

                    }
                }

            } else {
                print("We dont have any active timers")
            }

            // Remove active timers because we reset them up
            for timer in timers {
                managedContext.delete(timer)
            }
            do {
                print("deleted")
                try managedContext.save() // <- remember to put this :)
            } catch {
                // Do something... fatalerror
            }

        } catch let error as NSError {
          print("Could not fetch. \(error), \(error.userInfo)")
        }
    }

    @objc func applicationWillResign() {
        // handle event
        saveActiveTimer()
    }


    func saveActiveTimer() {
        if isTimeRunning {
            // Create a new alarm object
            guard let appDelegate =
              UIApplication.shared.delegate as? AppDelegate else {
              return
            }

            let context = appDelegate.persistentContainer.viewContext
            if let entity = NSEntityDescription.entity(forEntityName: "Timers", in: context) {

                let newTimer = NSManagedObject(entity: entity, insertInto: context)
                newTimer.setValue(true, forKey: "active")

                let now = Date()
                newTimer.setValue(now, forKey: "appCloseTime")
                newTimer.setValue(self.time, forKey: "alreadyTimed")

                do {
                   try context.save()
                    print("object saved success")
                  } catch {
                   print("Failed saving")
                }
            }
        }
    }
}

EDIT - 这里是在xCode 11.3和iOS 13.2的物理设备上的完整测试和工作代码 - 你必须找出如何根据你的按钮启动和停止计时器 - 但这个例子只是在应用程序第一次打开时启动计时器,而从未停止或重置它。

你可以通过创建一个新的单视图xCode项目并将其为你创建的第一个视图控制器中的代码替换为上面的代码来重现这种情况。然后创建一个标签来连接到出口 timerLabel 关于《公约》的

  • 同时确保在创建新项目时启用CoreData *然后在xcdatamodel文件中设置实体和属性。

CoreData setup

希望能帮到你

© www.soinside.com 2019 - 2024. All rights reserved.