如果用户拒绝iOS Swift中的位置权限,如何手动访问用户位置

时间:2019-05-24 09:35:36

标签: ios swift permissions mkmapview

即使用户拒绝了如何手动获取用户位置的权限,我也希望访问该用户的用户位置。

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    determineMyCurrentLocation()
}

func determineMyCurrentLocation() {
    locationManager = CLLocationManager()
    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.requestAlwaysAuthorization()

    locationManager.startUpdatingLocation()
    mapView.showsUserLocation = true
}

func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {

    if status == .authorizedWhenInUse || status == .authorizedAlways       {

locationManager.startUpdatingLocation()  }

}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let userLocation:CLLocation = locations[0] as CLLocation
   let center = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)

    let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.20, longitudeDelta: 0.20))
         print("user latitude = \(userLocation.coordinate.latitude)")
        print("user longitude = \(userLocation.coordinate.longitude)")
    self.mapView.setRegion(region, animated: true)
    self.mapView.showsUserLocation = true
}

func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
    print("error::: \(error)")

}

0 个答案:

没有答案