swift 3 如何获取通知授权状态?
How to get notification authorization status in swift 3?
如何在 iOS11 中查看 UNUserNotificationCenter 的当前授权状态?我找了一段时间并找到了一些代码,但它不在 swift 3 中,并且一些函数在 iOS 10 中被弃用。有人能帮忙吗?
好的,我找到了:
let center = UNUserNotificationCenter.current()
center.getNotificationSettings { (settings) in
if(settings.authorizationStatus == .authorized)
{
print("Push authorized")
}
else
{
print("Push not authorized")
}
}
编码方式:Kuba
在获取通知授权状态时,其实可以有三种状态,即
- 授权
- 拒绝
- 未定
检查这些的直接方法是使用 switch-case,其中 .authorized
、.denied
和 .nonDetermined
是 UNAuthorizationStatus
中的枚举
UNUserNotificationCenter.current().getNotificationSettings { (settings) in
print("Checking notification status")
switch settings.authorizationStatus {
case .authorized:
print("authorized")
case .denied:
print("denied")
case .notDetermined:
print("notDetermined")
}
}
UNAuthorizationStatus
的描述可以在 Apple 文档中找到 https://developer.apple.com/documentation/usernotifications/unauthorizationstatus
如何在 iOS11 中查看 UNUserNotificationCenter 的当前授权状态?我找了一段时间并找到了一些代码,但它不在 swift 3 中,并且一些函数在 iOS 10 中被弃用。有人能帮忙吗?
好的,我找到了:
let center = UNUserNotificationCenter.current()
center.getNotificationSettings { (settings) in
if(settings.authorizationStatus == .authorized)
{
print("Push authorized")
}
else
{
print("Push not authorized")
}
}
编码方式:Kuba
在获取通知授权状态时,其实可以有三种状态,即
- 授权
- 拒绝
- 未定
检查这些的直接方法是使用 switch-case,其中 .authorized
、.denied
和 .nonDetermined
是 UNAuthorizationStatus
UNUserNotificationCenter.current().getNotificationSettings { (settings) in
print("Checking notification status")
switch settings.authorizationStatus {
case .authorized:
print("authorized")
case .denied:
print("denied")
case .notDetermined:
print("notDetermined")
}
}
UNAuthorizationStatus
的描述可以在 Apple 文档中找到 https://developer.apple.com/documentation/usernotifications/unauthorizationstatus