Search code examples
iosiphoneswiftcllocationmanagerreactive-swift

Reactive Location - Handle success and failure block


It might be simple question. I'm using Reactive Location, to get user's current location, please find my below code,

    ReactiveLocation.authorizeAction.apply(.whenInUse).startWithResult {
        switch $0 {
        case let .success(status):
            print("Current user permission status on WhenInUse is \(status)")
        case let .failure(error):                
            print(error.localizedDescription)
        }
    }

Here error is .restricted and .denied, I want user to be presented with the error message according to error. How to identify it.

In above code, completion block looks like this,

enter image description here

Please help me to solve the issue.


Solution

  • I suggest learning more about Swift enum.

    You can check for .restricted and .denied the same way you checked for .success and .failure.

    The only difference is LocationAuthorizationError doesn't have associated values.

    ReactiveLocation.authorizeAction.apply(.whenInUse).startWithResult {
        switch $0 {
        case let .success(status):
            print("Current user permission status on WhenInUse is \(status)")
    
        case let .failure(actionError):
            switch actionError {
            case .producerFailed(.restricted):
                print("Authorization Restricted")
            case .producerFailed(.denied):
                print("Authorization Denied")
            default:
                break
            }
    
        }
    }