3

As UIApplication.sharedApplication() isn't available in the an iOS8 today widget/extension, how can I dynamically check if the code is currently running as widget or as app? When running the app I'd like to call e.g.

UIApplication.sharedApplication().registerUserNotificationSettings(settings)

When running the same code as widget this should simply be skipped. Is it possible to check for this?

Bernd
  • 11,133
  • 11
  • 65
  • 98

3 Answers3

1

Make a separate prefix file for your extension, define a macro in the prefix file that is not defined in the app's prefix file. Then checking if this macro is defined will tell you whether you are in the extension or not. Alternately you can define the macro in the app's prefix file and not in the extensions' and check in the other direction.

newacct
  • 119,665
  • 29
  • 163
  • 224
1

My solution:

#ifdef UIApplication
... //App
#else
... //Today Extension
#endif
liruqi
  • 1,524
  • 15
  • 12
0

I checked the bundle identifier of my host app to determine if I was running in it or my Today Extension.

For example:

    if Bundle.main.bundleIdentifier == "your.host.app.bundleIdentifier" {
        UIApplication.sharedApplication().registerUserNotificationSettings(setting)
    } else {
        // Today Extension
    }
Patrick Ridd
  • 401
  • 6
  • 4