Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/112.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
当minium目标平台为iOS 7.0时,如何在Swift中使用iOS 8.0 API?_Ios_Swift - Fatal编程技术网

当minium目标平台为iOS 7.0时,如何在Swift中使用iOS 8.0 API?

当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

今天,我在学习通知。在注册表通知设置(:)定义下,在UIApplication类引用中,它说

在安排任何本地通知之前,您必须调用registerUserNotificationSettings方法,让系统知道您计划向用户显示哪些类型的警报(如果有)


但注册表通知设置仅在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
}
看答案和问题应该解决你的问题。看答案和问题应该解决你的问题。