Desmond
Desmond

Reputation: 5001

authorizationStatus return a bool value

I wasn't sure if this is possible.

I would like to have a bool returned from authorizationStatus when i called UNUserNotificationCenter.current().getNotificationSettings()

I first created a callback block to determine authorizationStatus then the next function will return a bool back to the original caller.

I'm encountering non-void return value in void function as usual as my callback is returning a void.

Ideally, I would like to use checkNotificationBlocks()just like the method: 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 }
    }

Upvotes: 0

Views: 476

Answers (1)

Desmond
Desmond

Reputation: 5001

I eventually did updated my code to get my desired outcome. The semaphore obtains a lock when it makes wait call and it's released when it's signaled from the asynchronous 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
}

Upvotes: 2

Related Questions