gpt4 book ai didi

ios - 当最小目标平台是 iOS 7.0 时,如何在 Swift 中使用 iOS 8.0 API?

转载 作者:搜寻专家 更新时间:2023-11-01 06:20:57 24 4
gpt4 key购买 nike

今天,我在学习Notification。在 registerUserNotificationSettings(_:) 定义下,在 UIApplication 类引用中,它说

"Prior to scheduling any local notifications, you must call the registerUserNotificationSettings method to let the system know what types of alerts, if any, you plan to display to the user."

registerUserNotificationSettings 仅适用于 iOS 8.0 或更高版本。那么,如果我使用 Swift,如何使其与 iOS 7 兼容?

最佳答案

您可以在 Swift 2 中使用可用性检查并在 iOS 7 中使用 registerForRemoteNotifications

示例:

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)
}

关于ios - 当最小目标平台是 iOS 7.0 时,如何在 Swift 中使用 iOS 8.0 API?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34359375/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com