单击"允许"后未调用didChangeAuthorizationStatus

Ami*_*r_P 9 cllocationmanager ios swift

这是我实现谷歌地图的代码,并且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()
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

单击允许或不允许didChangeAuthorizationStatus从未调用后

Ele*_*ena 12

如果您使用的是Swift 3,则此方法的整个签名不正确.

这就是你需要的:

func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) { 
    //your code 
}
Run Code Online (Sandbox Code Playgroud)

  • 如果这些东西是在扩展中声明的,编译器应该检测到这些东西 (2认同)
  • 我想说一句话!这里我不能说。 (2认同)