当minium目标平台为iOS 7.0时,如何在Swift中使用iOS 8.0 API?
今天,我在学习通知。在注册表通知设置(:)定义下,在UIApplication类引用中,它说 在安排任何本地通知之前,您必须调用registerUserNotificationSettings方法,让系统知道您计划向用户显示哪些类型的警报(如果有)当minium目标平台为iOS 7.0时,如何在Swift中使用iOS 8.0 API?,ios,swift,Ios,Swift,今天,我在学习通知。在注册表通知设置(:)定义下,在UIApplication类引用中,它说 在安排任何本地通知之前,您必须调用registerUserNotificationSettings方法,让系统知道您计划向用户显示哪些类型的警报(如果有) 但注册表通知设置仅在iOS 8.0或更高版本中可用。那么,如果我使用Swift,如何使其与iOS 7兼容呢?您可以在Swift 2中使用可用性检查,并在iOS 7中使用registerForRemoteNotificationTypes 例如: if
但注册表通知设置仅在iOS 8.0或更高版本中可用。那么,如果我使用Swift,如何使其与iOS 7兼容呢?您可以在Swift 2中使用可用性检查,并在iOS 7中使用registerForRemoteNotificationTypes 例如:
if #available(iOS 8.0, *) {
let options: UIUserNotificationType = [
UIUserNotificationType.Alert,
UIUserNotificationType.Badge,
UIUserNotificationType.Sound
]
let settings = UIUserNotificationSettings(forTypes: options, categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(settings)
} else {
let options: UIRemoteNotificationType = [UIRemoteNotificationType.Badge, UIRemoteNotificationType.Sound, UIRemoteNotificationType.Alert]
UIApplication.sharedApplication().registerForRemoteNotificationTypes(options)
}
您可以在Swift 2中使用可用性检查,并在iOS7中使用registerForRemoteNotificationTypes 例如:
if #available(iOS 8.0, *) {
let options: UIUserNotificationType = [
UIUserNotificationType.Alert,
UIUserNotificationType.Badge,
UIUserNotificationType.Sound
]
let settings = UIUserNotificationSettings(forTypes: options, categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(settings)
} else {
let options: UIRemoteNotificationType = [UIRemoteNotificationType.Badge, UIRemoteNotificationType.Sound, UIRemoteNotificationType.Alert]
UIApplication.sharedApplication().registerForRemoteNotificationTypes(options)
}
将代码放入if语句中以检查ios版本
if #available(iOS 8, *) {
// ios 8 and newer
}
else {
// ios 7 and older
}
将代码放入if语句中以检查ios版本
if #available(iOS 8, *) {
// ios 8 and newer
}
else {
// ios 7 and older
}
看答案和问题应该解决你的问题。看答案和问题应该解决你的问题。