Swift:如果用户接受或拒绝通知,则执行操作

时间:2016-11-06 15:52:30

标签: ios swift nsusernotification

我使用以下代码请求推送和本地通知权限:

let application = UIApplication.shared
let settings: UIUserNotificationSettings = UIUserNotificationSettings( types:  [.alert, .badge, .sound], categories: nil )

application.registerUserNotificationSettings( settings )
application.registerForRemoteNotifications()

在采取行动之前,我需要等待用户接受或拒绝通知。我怎样才能做到这一点?

2 个答案:

答案 0 :(得分:3)

注意: Apple已经弃用了我在下面给出的答案。请参阅@ ergunkocak的回答

当用户授予或拒绝权限时,应用代理中的回调方法为application(_:didRegister:),这是您应根据用户选择的权限设置采取特定操作的方法。我建议阅读文档here.

答案 1 :(得分:2)

AppDelegate.swift中的Swift 3:

func application(_ application: UIApplication, didRegister notificationSettings: UIUserNotificationSettings) {
    // Check here
}