Make sure iOS10 or lower at compile time

I am introducing new notifications with UNUserNotificationCenter. But I need to keep it backward compatible, so I have checks everywhere:

if #available(iOS 10.0, *) { ... } else { ... } 

Which seems to work fine in iOS10. To be able to use the UNUserNotificationCenter structure, I need to import:

 import NotificationCenter 

But it crashes iOS9.3 because it does not know what it is. This is a compile-time action, not a run-time action โ€” which means I cannot put a condition on the import. If I create a separate class and put

 @available(iOS 10.0, *) class .... 

import also occurs before class implementation. How do I solve this problem?

+5
source share
2 answers

Try moving on to creating phases-> Link binaries to libraries and add NotificationCenter and set the status to โ€œoptionalโ€ rather than โ€œrequiredโ€.

+1
source

First of all you should use:

Import UserNotifications for local notifications.

: Xcode 8 beta 6 and Sierra beta6

some test code:

a) iOS 10 only for quick 3:

 override func viewDidLoad() { super.viewDidLoad() // ADC site: // Listing 1 // Requesting authorization for user interactions 

/ * allow center = UNUserNotificationCenter.currentNotificationCenter () center.requestAuthorizationWithOptions ([. Alert, .Sound]) {(provided, error) in} * /

  // swift 3.0: let center = UNUserNotificationCenter.current() center.requestAuthorization(options: [.alert, .sound]) { (granted, error) in // Enable or disable features based on authorization. } 

2) iOS 9 support: (I changed the deployment target)

  // swift 3.0: if #available(iOS 10.0, *) { let center = UNUserNotificationCenter.current() center.requestAuthorization(options: [.alert, .sound]) { (granted, error) in // Enable or disable features based on authorization. } } else { // Fallback on earlier versions } 
0
source

All Articles