Conditional Compilation for User Notifications in Swift 3 and Xcode 8.2

We have an application supporting iOS 9+, that is being developed in Swift 3 and Xcode 8.2.x. iOS 10 introduced a new method for registering for push notifications, described well in this post. Since iOS 9 doesn’t know about the User Notifications framework, we want to use the iOS 9 method when the device is iOS 9, and use the UserNotifications.framework on iOS 10. In Objective C, we would accomplish this via conditional compilation (e.g. #ifdef statements).

In Swift 3, there is some facility for conditional compilation #available and #if. We try to wrap the branches of notification registration code in either of these directives, and the app will not compile.compilation error
#available generates similar results

  • Send App to Background process
  • Xcode/LLDB: How to get information about an exception that was just thrown?
  • Image does not update when changed if that image is used on LaunchScreen.storyboard
  • Can't build XCode 4 Project from Textmate
  • Could not load NIB in bundle
  • Is it possible to ctrl-drag in Storyboard to set a UINavigationControllerDelegate?
  • This is the case when targeting generic/real devices. The app will compile against simulators.

    Solutions Collect From Internet About “Conditional Compilation for User Notifications in Swift 3 and Xcode 8.2”

    Based on your comments above, you want a runtime check, not conditional compilation, and #available is precisely that.

    This works fine for me:

    if #available(iOS 10.0, *) {
        UNUserNotificationCenter.current().requestAuthorization(options: [.badge, .alert, .sound]) {
            (granted, error) in
    else {
        UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [.badge, .sound, .alert], categories: nil))

    UIUserNotificationSettings is deprecated in iOS 10, but it’s actually still there. Apple doesn’t typically remove APIs until they’ve been deprecated for a very long time.

    I suspect you have some other kind of build issue. Off the top of my head: do you have Base SDK and Deployment Target set correctly?