gpt4 book ai didi

ios - authorizationStatus 返回一个 bool 值

转载 作者:行者123 更新时间:2023-11-28 11:54:20 28 4
gpt4 key购买 nike

我不确定这是否可能。

当我调用 UNUserNotificationCenter.current().getNotificationSettings()

时,我希望从 authorizationStatus 返回一个 bool 值

我首先创建了一个回调 block 来确定 authorizationStatus 然后下一个函数将返回一个 bool 返回给原始调用者。

我像往常一样在 void 函数中遇到非 void 返回值,因为我的回调返回 void。

理想情况下,我想使用 checkNotificationBlocks() 就像方法一样:isNotificationEnabled()

func checkNotificationBlocks(callback: @escaping (Bool) -> Void)
{
UNUserNotificationCenter.current().getNotificationSettings()
{
settings in
var status = false
switch settings.authorizationStatus
{
case .authorized: status = true
case .denied: status = false
case .notDetermined: status = false
}

if UIApplication.shared.isRegisteredForRemoteNotifications && status == true
{callback(true) }
else { callback(false) }
}

}

func isNotificationEnabledBlocks() -> Bool
{
checkNotificationBlocks {
b in
if b == true { return true } //Unexpected non-void return value in void function
else { return false } //Unexpected non-void return value in void function
}
}
//currentUserNotificationSettings' was deprecated in iOS 10.0:
func isNotificationEnabled() -> Bool
{
if UIApplication.shared.isRegisteredForRemoteNotifications && settings.types.rawValue != 0
{ print("isNotificationEnabled return true"); return true }
else { print("isNotificationEnabled return false");return false }
}

最佳答案

我最终确实更新了我的代码以获得我想要的结果。信号量在进行等待调用时获得锁定,并在从异步 block 发出信号时释放。

 func isNotificationEnabledBlocks() -> Bool
{
let waitOut = DispatchSemaphore(value: 0)
var checks = false

checkNotificationBlocks {
b in checks = b
waitOut.signal()
}
waitOut.wait()
return UIApplication.shared.isRegisteredForRemoteNotifications && checks == true ? true : false
}

关于ios - authorizationStatus 返回一个 bool 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51357826/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com