2016-08-12 47 views
5

我正在使用UNUserNotificationCenter實施新通知。但我需要保持向後兼容,因此我必須檢查所有的地方:在編譯時檢查iOS10或更低版本

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

這似乎在iOS10做工精細。爲了能夠使用UNUserNotificationCenter框架,我不得不進口:

import NotificationCenter 

但它崩潰了iOS9.3,因爲它不知道它是什麼。 這是一個編譯時的動作,而不是運行時的動作 - 所以這意味着我不能把條件對我創建一個單獨的類imports.If,並把

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

那裏的進口也在課前發生的事情實現。 我應該如何解決這個問題?

+0

你得到這一點,因爲你進口的 「通知中心」? – MCMatan

回答

1

嘗試導航到構建階段 - >與庫鏈接二進制文件並添加NotificationCenter並將狀態設置爲「可選」而不是「必需」。

+0

你可能意思是「建立階段」,我把它放在那裏,沒有任何改變。 –

+0

含義:我無法從類中移除輸入,它仍然需要它。難道我做錯了什麼? –

0

首先你必須使用:

進口UserNotifications 本地通知。

條件:Xcode的8個β6和塞拉利昂beta6

一些測試代碼:

一個)的iOS 10僅用於迅速3:

override func viewDidLoad() { 
    super.viewDidLoad() 

    // ADC site: 
    // Listing 1 
    // Requesting authorization for user interactions 

/*讓中心= UNUserNotificationCenter.currentNotificationCenter( ) center.requestAuthorizationWithOptions([。Alert,.Sound]){(granted,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:(我已經改變了部署目標)

// 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 
    }