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

Rag*_*dra 2 uilocalnotification swift ios8

我正在使用Swift for iOS8编写我的应用程序.

我想使用UILocalNotification来通知用户一些事情.为此,我已在应用程序启动时请求权限:

application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: UIUserNotificationType.Sound | UIUserNotificationType.Alert | UIUserNotificationType.Badge, categories: nil))
Run Code Online (Sandbox Code Playgroud)

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

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

kur*_*amo 8

..这就是你在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")
    }
Run Code Online (Sandbox Code Playgroud)

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