user528432
user528432

Reputation: 421

Pausing timer when app is in background state Swift

My ViewController.swift

func startTimer() {
    timer = NSTimer().scheduleTimerWithTimerInvterval(1.0,target: self,selctor: Selector("couting"),userinfo: nil, repeats: true)
}

func pauseTimer() {
    timer.invalidate()
    println("pausing timer")
}

and this is appDelegate.swift

func applicateWillResignActive(application: UIApplication) {
    viewController().pauseTimer()
    println("closing app")
}

It is printing pausing timer and closing app but when I open again I see it never paused. How do I do it correctly?

Upvotes: 14

Views: 13969

Answers (5)

Yunus Karakaya
Yunus Karakaya

Reputation: 531

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")
}

Upvotes: 7

YungGoat
YungGoat

Reputation: 143

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.
}

Upvotes: 1

Brian Bird
Brian Bird

Reputation: 1206

Accepted answer by @Suresh in Swift 3

Set an observer listening to when the application did enter background in your ViewController's viewDidLoad() method.

 NotificationCenter.default.addObserver(self, selector: #selector(myObserverMethod), name:NSNotification.Name.UIApplicationDidEnterBackground, object: nil)

Add the below function to receive the notification.

 func myObserverMethod(notification : NSNotification) {

   print("Observer method called")

  //You may call your action method here, when the application did enter background.
  //ie., self.pauseTimer() in your case.

}

Upvotes: 3

Suresh Kumar Durairaj
Suresh Kumar Durairaj

Reputation: 2126

You have to set an observer listening to when the application did enter background. Add the below line in your ViewController's viewDidLoad() method.

NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("myObserverMethod:"), name:UIApplicationDidEnterBackgroundNotification, object: nil)

Add the below function to receive the notification.

func myObserverMethod(notification : NSNotification) {
    println("Observer method called")

    //You may call your action method here, when the application did enter background.
    //ie., self.pauseTimer() in your case.
}

Happy Coding !!!

Upvotes: 29

Midhun MP
Midhun MP

Reputation: 107231

You are creating a new object and calling pauseTimer() on it:

viewController().pauseTimer()   // This line causes issue viewController(), creates a new instance

Instead of creating new object, either you should pass that instance to AppDelegate and call pauseTimer() on existing object or Listen for UIApplicationDidEnterBackgroundNotification notification in your view controller class and pause timer from there.

Upvotes: 0

Related Questions