Pausing timer when app is in background state Swift

后端 未结 5 875
攒了一身酷
攒了一身酷 2021-01-02 05:57

My ViewController.swift

func startTimer() {
    timer = NSTimer().scheduleTimerWithTimerInvterval(1.0,target: self,selctor: Selector(\"couting\"),userinfo: n         


        
5条回答
  •  有刺的猬
    2021-01-02 06:36

    Updated Answer for Swift 4:

    override func viewDidLoad() {
        super.viewDidLoad()
        NotificationCenter.default.addObserver(self, selector: #selector(myObserverMethod), name:NSNotification.Name.UIApplicationDidEnterBackground, object: nil)
    }
    
    @objc func myObserverMethod() {
        // Call your action method here, when the application did enter background.
    }
    

提交回复
热议问题