I have a method that is used in several places of an app I'm working on. It's a method to check if remote push notifications are enabled or not. The method returns a value but as you may know currentUserNotificationSettings
was deprecated so now I'm using getNotificationSettings
.
The problem is the first returns a value and the latests uses a block. I want to still be able to return a value to avoid refactoring everything so I wrote the following but it's failing and I can't understand why...
Is this ok?!
public static var isRemoteEnabled: Bool {
var notificationSettings: UNNotificationSettings?
let semasphore = DispatchSemaphore(value: 2)
UNUserNotificationCenter.current().getNotificationSettings { setttings in
notificationSettings = setttings
semasphore.signal()
}
semasphore.wait()
guard let authorizationStatus = notificationSettings?.authorizationStatus else { return false }
return authorizationStatus == .authorized
}
Edited:
I followed @rmaddy comment and at least now it doesn't crash but it gets stuck in the wait()
. If I go to debugger and e semasphore.signal()
it completes and the app continues working fine. Somehow the completion block is not being called.