Search code examples
iosios8ios-app-extensiontoday-extension

Dose NSTimer can be used in Today Extension (Widget)?


I want to use a NSTimer in Today Extension to refresh the widget, but I don't know dose this behaviour is legality?

I was make a demo use a NSTimer to counting and refresh the widget UI every 1 second, seem like it's work, but the problem is when user exit Notification Center, the timer is still running, When and Where should I stop the timer?


Solution

  • I can also confirm that this approach works well for me.

    override func viewDidDisappear(animated:Bool) {
        // stop the timer here when notification center is closed
    }
    

    Interestingly, both viewWillDisappear and viewDidDisappear are both called together one after another (in that order) as soon as you start to swipe up to close the notification center. This means that while notification center and your widget are still visible on the screen (if you keep your finger on the drag handle) viewDidDisappear is already called.