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.
#available
generates similar results
This is the case when targeting generic/real devices. The app will compile against simulators.