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])
}
您的代码是正确的。只需将iOS 9,8和7合并为一个块即可。 iOS 10在单独的块中。 – Basheer
如果我这样做,如果新发布的iOS版本投入使用,该代码是否仍然有效?谢谢您的回答。 –
您正在检查#available标记,它将验证设备的操作系统版本并执行该块。从iOS 10到现在(iOS 11),仍然使用UNUserNotification类。一旦苹果改变了这个框架,你必须像现在为iOS 10那样支持新的框架。 – Basheer