iosswifttoday-extensionios8-today-widget

Determine if the widget is enabled


Is there any way to determine if my Today Widget is already added to Notification Centre by user? I need to know so I can change some Labels in host app accordingly.


Solution

  • There is no API for that, but you could have your today widget write something to the shared container that you can read from your app to determine if it's been displayed. The main problems with that are that it won't happen until the widget has been displayed at least once, and you can't relly tell if they've installed and then removed it.

    func widgetHasRun() {
        if let sharedContainer = NSUserDefaults(suiteName: "group.com.my.app") {
            sharedContainer.setBool(true, forKey: "today widget installed")
            sharedContainer.synchronize()
        }
    }
    

    We use this technique to determine whether we should prompt new users to install our widget.