Check for LocationManager authorization

PHOTO EMBED

Wed Jun 02 2021 13:30:53 GMT+0000 (UTC)

Saved by @joeavargas

import UIKit
import CoreLocation

class ViewController: UIViewController{
  //...
}

extensiion ViewController: CLLocationManagerDelegate{
  
  func checkLocationAuthorization(){
        switch locationManager.authorizationStatus {
        case .authorizedWhenInUse:
            // do GPS location stuff
            getUserCoordinates()
            locationManager.startUpdatingLocation()
            break
        case .denied:
            // Show an alert instructing user how to turn on permission
            break
        case.notDetermined:
            locationManager.requestWhenInUseAuthorization()
            break
        case .restricted:
            // Show an alert instructing user how to turn on permission
            break
        default:
            break
        }
    }
  
  //...
  
  func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
        checkLocationAuthorization()
    }
}
content_copyCOPY