gpt4 book ai didi

swift - iOS8 - UILocalNotification,稍后从代码中检查权限

转载 作者:可可西里 更新时间:2023-10-31 23:55:27 27 4
gpt4 key购买 nike

我正在为 iOS8 使用 Swift 编写我的应用程序。

我想使用 UILocalNotification 来通知用户一些事情。为此,我已在应用启动时请求许可:

application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: UIUserNotificationType.Sound | UIUserNotificationType.Alert | UIUserNotificationType.Badge, categories: nil))

确认框显示请求许可。如果用户允许,则没有问题。

如果用户未授予权限,我如何检查是否从代码(以编程方式)启用了本地通知?

最佳答案

..这就是你在 Swift 中检查它的方式:

let currentSettings = UIApplication.sharedApplication().currentUserNotificationSettings()
let required:UIUserNotificationType = UIUserNotificationType.Sound; // Add other permissions as required
let notification = UILocalNotification()

if (currentSettings.types & required) != nil {
println("sound will come and also vibration")
notification.soundName = UILocalNotificationDefaultSoundName
}else{
println("no sound no vibration")
}

很高兴知道:
它会自动振动,无论声音是通过您的 iphone 按钮打开/关闭 - 如果用户在您的应用程序设置中将声音设置为关闭,则不会发生任何事情。
因此您不必另外添加振动。

关于swift - iOS8 - UILocalNotification,稍后从代码中检查权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25945939/

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