12

This is my code for implementing google map and CLLocationManager:

class MapViewController: UIViewController {
    @IBOutlet weak var MapView: GMSMapView!
    var locationmanager = CLLocationManager()

    override func viewDidLoad() {
        super.viewDidLoad()
        locationmanager.delegate = self
        locationmanager.requestWhenInUseAuthorization()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}
extension MapViewController: CLLocationManagerDelegate {
    private func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
     print("didChangeAuthorizationStatus")
     if status == .authorizedWhenInUse {
            locationmanager.startUpdatingLocation()
            MapView.isMyLocationEnabled = true
            MapView.settings.myLocationButton = true
        }
    }

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        print("didUpdateLocations")
        if let location = locations.first {
            MapView.camera = GMSCameraPosition(target: location.coordinate, zoom: 15, bearing: 0, viewingAngle: 0)
            locationmanager.stopUpdatingLocation()
        }
    }
}

after clicking on Allow or Not Allow didChangeAuthorizationStatus never called

Amir_P
  • 8,322
  • 5
  • 43
  • 92

1 Answers1

14

If you are on Swift 3 the whole signature of this method is not correct.

This is what you need:

func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) { 
    //your code 
}
Elena
  • 829
  • 8
  • 20
  • 1
    these things should be detected by the compiler if its declared inside an extension – some_id May 19 '17 at 14:15
  • @Jarryd unfortunately Xcode compiler's not perfect. Things may have changed since this question was posted (half a year ago) – Elena May 19 '17 at 19:30
  • I was implementing a location manager the other day and compiler complained about marking the function private in the extension. The function would only be called once for some reason. Anyway, this fixed it, thanks! – some_id May 20 '17 at 18:19
  • 1
    I would like to say something! I can not say here. – Hope Sep 25 '18 at 11:20