检查UILocalNotification弃用后是否启用了用户通知 [英] Check whether user notifications are enabled after UILocalNotification deprecation
问题描述
在我的应用中,我希望能够检查用户是否启用了通知。在iOS 10中,我是使用委托检查的。
In my app, I want to be able to check if the user has notifications enabled or not. In iOS 10, I did this using a check in the delegate.
此检查现已弃用,我想对其进行更新,但我不知道该怎么做。在iOS 11中使用。
This check is now deprecated and I want to update it, but I can't figure out what to use in iOS 11.
弃用警告如下:
currentUserNotificationSettings在iOS 10.0中已弃用:使用
UserNotifications框架的-[UNUserNotificationCenter
getNotificationSettingsWithCompletionHandler:]和
-[UNUserNotificationCenter getNotificationCategoriesWithCompletionHandler:]
currentUserNotificationSettings' was deprecated in iOS 10.0: Use UserNotifications Framework's -[UNUserNotificationCenter getNotificationSettingsWithCompletionHandler:] and -[UNUserNotificationCenter getNotificationCategoriesWithCompletionHandler:]
我试图借助此警告来更新代码,但我无法弄清楚。
I've tried to update the code with the help of this warning but I can't figure it out.
如果有人可以建议无论如何,获得像这样的支票会很有帮助。谢谢,下面是我为iOS 10使用的代码。
If anyone can suggest anyway to get a check like this working it would help a lot. The code I have been using for iOS 10 is below, thanks.
let notificationType = UIApplication.shared.currentUserNotificationSettings!.types
if notificationType == [] {
print("Notifications are NOT enabled")
} else {
print("Notifications are enabled")
}
推荐答案
步骤1:导入UserNotifications
步骤2:
UNUserNotificationCenter.current().getNotificationSettings { (settings) in
if settings.authorizationStatus == .authorized {
// Notifications are allowed
}
else {
// Either denied or notDetermined
}
}
检查设置对象以获取更多信息。
Inspect the settings object for more informations.
这篇关于检查UILocalNotification弃用后是否启用了用户通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!