2017-08-23 38 views
0

Swift 3 IOS 10:我一直在尋找允許推送通知的代碼;最後,我可以從www.codementor.io找到一個非常有用的。但是,我陷入了困惑。我想知道下面的代碼是否適用於較低或較新版本的iOS。由於蘋果將不懈地發佈它的版本,下面的代碼將如何處理這些變化?有沒有辦法解決我提到的問題?允許推送通知版本控制

// iOS 10 support 

if #available(iOS 10, *) { 

UNUserNotificationCenter.current().requestAuthorization(options:[.badge, .alert, .sound]){ (granted, error) in } 

application.registerForRemoteNotifications() 

} 

// iOS 9 support 

else if #available(iOS 9, *) { 

UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [.badge, .sound, .alert], categories: nil)) 

UIApplication.shared.registerForRemoteNotifications() 

} 

// iOS 8 support 

else if #available(iOS 8, *) { 

UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [.badge, .sound, .alert], categories: nil)) 

UIApplication.shared.registerForRemoteNotifications() 

} 

// iOS 7 support 

else { 

application.registerForRemoteNotifications(matching: [.badge, .sound, .alert]) 

} 
+0

您的代碼是正確的。只需將iOS 9,8和7合併爲一個塊即可。 iOS 10在單獨的塊中。 – Basheer

+0

如果我這樣做,如果新發布的iOS版本投入使用,該代碼是否仍然有效?謝謝您的回答。 –

+0

您正在檢查#available標記,它將驗證設備的操作系統版本並執行該塊。從iOS 10到現在(iOS 11),仍然使用UNUserNotification類。一旦蘋果改變了這個框架,你必須像現在爲iOS 10那樣支持新的框架。 – Basheer

回答

0

是適用於iOS 10的代碼將適用於iOS 11正常工作。

基本上#available宏檢查最小OS所以它也是安全的合併的iOS 8和9

//iOS 10+ 
if #available(iOS 10, *) { 
    UNUserNotificationCenter.current().requestAuthorization(options:[.badge, .alert, .sound]){ (granted, error) in 
     if (granted) { //check if authorization was granted before registering 
      application.registerForRemoteNotifications() 
     } 
    } 
} 

// iOS 8+ support 
else if #available(iOS 8, *) { 
    UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [.badge, .sound, .alert], categories: nil)) 
    UIApplication.shared.registerForRemoteNotifications() 
} 

// iOS 7 support 
else { 
    application.registerForRemoteNotifications(matching: [.badge, .sound, .alert]) 
} 
0

試試這個:

func setUpPushNotification(application: UIApplication) { 
     if #available(iOS 10.0, *) { 
     let notificationTypes: UNAuthorizationOptions = [.alert, .badge, .sound] 
     UNUserNotificationCenter.current().requestAuthorization(
      options: notificationTypes, 
      completionHandler: {_,_ in }) 
     // For iOS 10 display notification (sent via APNS) 
     UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate 
     } 
     else{ 
      let notificationTypes: UIUserNotificationType = [.alert, .badge, .sound] 
      let pushNotificationSettings = UIUserNotificationSettings(types: notificationTypes, categories: nil) 
      application.registerUserNotificationSettings(pushNotificationSettings) 

     } 
      application.registerForRemoteNotifications() 
    } 
+0

此代碼是否可以與所有版本一起使用? –

0
if #available(iOS 10, *) 

讀爲:如果從iOS的版本10或以上是可利用的。

所以,如果你留下你的代碼和iOS 11,12等,他們將全部進入這個if分支。

所以只要Push API沒有改變,你的代碼就會工作。 如果蘋果將來改變了API(例如在iOS 12中),您將不得不添加另一個if分支並將您的應用重新提交給商店...

(如果有人使用iOS 9使用您的應用程序,將在第二個if分支結束 - 因爲第一個不適用,因此if的順序可以保證較低的iOS版本獲得正確的代碼。)