目前我有以下代码,主要是要求用户发送通知的权限,然后检查通知是否已启用:
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound], completionHandler: { (granted, error) in
if granted {
} else {
}
})
let notifType = UIApplication.shared.currentUserNotificationSettings?.types
if notifType?.rawValue == 0 {
print("being called")
let alert = UIAlertController(title: "Notifications are disabled for this app.", message: "Please enable notifications for the app to work properly. This can be done by going to Settings > Notifications > NAME HERE and allow notifications.", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.cancel, handler: nil))
show(alert, sender: self)
} else {
//enabled
}
代码可以工作,但是,在用户可以选择"是"之前,它会检查是否启用了通知。或"不"。因此,无论选择什么,弹出对话框。有没有办法可以等到用户选择"是"或"不"?
答案 0 :(得分:2)
您可以将支票移至回调中,因此首先检查授权:
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound], completionHandler: { (granted, error) in
if (granted) {
// Alert here
} else {
// Or here
}
})