Pausing timer when app is in background state Swift

后端 未结 5 920
攒了一身酷
攒了一身酷 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:15

    Updated Answer for Swift 5:

    NotificationCenter.default.addObserver
                  (self,
                   selector: #selector(myObserverMethod),
                   name:UIApplication.didEnterBackgroundNotification, object: nil)
    
    @objc func myObserverMethod(){
        print("Write Your Code Here")
    }
    

提交回复
热议问题