Sridhar
Sridhar

Reputation: 2228

iOS 13 How to check user is given only Always allow location permission

In my application, I want to check whether user is granted location permission "Always allow". Because I am doing kind of location sharing as like WhatsApp location live sharing.

Actually the test case is, when we change the location permission status to "while using the app" in the app settings and check the status on button click inside the app, I am getting "authorised Always" value.

  func isAlwaysPermissionGranted() -> Bool{
    let aStatus = CLLocationManager.authorizationStatus()
    if aStatus == .authorizedAlways {
        return true
    }
     return false
  }

If we have "while using the app" permission, I can't differentiate the "While Using the app" and "Always". Both are having same enum values that is "authorizedAlways".

But in WhatsApp, If I changed the location permission to "While Using the App" and tried to share my live location, WhatsApp shows one alert to change the location permission in App Settings.

I want to do same like that.

Kindly help me to different in code level.

Upvotes: 3

Views: 4288

Answers (3)

Sridhar
Sridhar

Reputation: 2228

Actually, I removed CLLocationManager.requestAlwaysAuthorization() from the code.

If you request for Always Authorization, CLLocationManager.authorizationStatus becomes authorizedAlways.

I made it like if app wants always access, user has to go to settings and give the always permission manually

Upvotes: 0

Asperi
Asperi

Reputation: 257663

You have to request explicitly via CLLocationManager.requestAlwaysAuthorization() (doc) and listen as stated by Apple documentation in delegate callback

Important

Use of authorizationStatus() is unnecessary and discouraged. Instead, implement the locationManager(_:didChangeAuthorization:) delegate callback to receive up-to-date authorization status.

Listen in delegate will give you possibility always be informed about any run-time changes of authorization status and make corresponding handling.

Upvotes: 0

Dilan
Dilan

Reputation: 2688

you can check it in didChangeAuthorization delegate method like this.

switch CLLocationManager.authorizationStatus() {
        case .notDetermined:
            //Ask for permission
            break
        case .restricted:
            // user retricted to use location service.(Ex: parental control)
            break
        case .denied:
            //user denied location service
            break
        case .authorizedAlways:
            //always allow
            break
        case .authorizedWhenInUse:
            // when in use
            break
        @unknown default:
            break
        }

Upvotes: 2

Related Questions