我有下面的代码,它应该返回NotificationCenter的设置,但是当我运行这段代码时,变量notificationSetting什么也不返回。
如何解决这个问题,以便应用程序等待结果?
func getNotificationSetting() -> String{
var notificationSetting = ""
UNUserNotificationCenter.current().getNotificationSettings { (settings) in
switch settings.authorizationStatus {
case .authorized, .provisional:
notificationSetting = "Authorized"
case .denied:
notificationSetting = "Denied"
case .notDetermined:
notificationSetting = "NotDetermined"
@unknown default:
notificationSetting = "NotDetermined"
}
}
return notificationSetting
}发布于 2020-03-12 21:25:51
getNotificationSettings异步执行。
func getNotificationSetting(completionHandler: @escaping (String) -> Void) {
UNUserNotificationCenter.current().getNotificationSettings { (settings) in
switch settings.authorizationStatus {
case .authorized, .provisional:
completionHandler("Authorized")
case .denied:
completionHandler("Denied")
case .notDetermined:
completionHandler("NotDetermined")
@unknown default:
completionHandler("NotDetermined")
}
}
}
func getSettings() {
self.getNotificationSetting(completionHandler: { (notificationSetting) in
// do what you want
print(notificationSetting)
})
}https://stackoverflow.com/questions/60661840
复制相似问题