2017-08-23 40 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版本获得正确的代码。)