2016-02-11 52 views
0

錯誤:requestWhenInUseAuthorization錯誤只在第一次啓動

Trying to start MapKit location updates without prompting for location authorization. Must call -[CLLocationManager requestWhenInUseAuthorization] or -[CLLocationManager requestAlwaysAuthorization] first.

代碼:

class ViewController: UIViewController { 

    @IBOutlet weak var mapView: MKMapView! 
    var locationManager: CLLocationManager! 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     locationManager = CLLocationManager() 
     locationManager.requestAlwaysAuthorization() 

     let currentLocationCoo = locationManager.location?.coordinate 
     if let locationCoo = currentLocationCoo{ 
      mapView.region = MKCoordinateRegionMake(CLLocationCoordinate2D(latitude: locationCoo.latitude, longitude: locationCoo.longitude), MKCoordinateSpan(latitudeDelta: 0.002, longitudeDelta: 0.002)) 
      print("location ni nil") 
     }else{ 


     mapView.region = MKCoordinateRegionMake(CLLocationCoordinate2D(latitude: 52.05695, longitude: 14.50575), MKCoordinateSpan(latitudeDelta: 0.002, longitudeDelta: 0.002)) 
     } 

     let defaultAnnotation = MKPointAnnotation() 
     defaultAnnotation.coordinate = mapView.region.center 
     defaultAnnotation.title="Center" 
     defaultAnnotation.subtitle="This is!" 
     mapView.addAnnotation(defaultAnnotation) 

     mapView.showsUserLocation=true 
     mapView.showsBuildings=true 
     mapView.userTrackingMode = .None 

    } 

我注意到,僅適用於iOS在第一次推出引發錯誤。在我甚至沒有同意位置服務提示之前。在隨後的發佈中,它工作正常。是不是因爲第一個視圖控制器的應用程序已經啓動使用地圖?解決方法可能不是直接跳到地圖,但首先表現出一些其他的VC。任何其他解決方法?

回答

1

是的,你是正確的,mapView.showsUserLocation=true需要訪問您的當前位置,這是不可能不同意由操作系統來顯示警報。您可以實現的CLLocationManagerDelegatedidChangeAuthorizationStatus方法,讓您的地圖與裏面的代碼。嘗試下面的代碼。

class ViewController: UIViewController,CLLocationManagerDelegate { 

    @IBOutlet weak var mapView: MKMapView! 
    var locationManager: CLLocationManager! 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     locationManager = CLLocationManager() 
     locationManager.delegate = self 
     locationManager.requestAlwaysAuthorization() 
    } 

    func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) { 
     switch status { 
     case .AuthorizedWhenInUse: 
      updateMap() 
      break 
     case .AuthorizedAlways: 
      updateMap() 
      // write your map related code here 
      break 
     default: 
      break 
     } 
    } 

    func updateMap() { 
     let currentLocationCoo = locationManager.location?.coordinate 
     if let locationCoo = currentLocationCoo{ 
      mapView.region = MKCoordinateRegionMake(CLLocationCoordinate2D(latitude: locationCoo.latitude, longitude: locationCoo.longitude), MKCoordinateSpan(latitudeDelta: 0.002, longitudeDelta: 0.002)) 
      print("location ni nil") 
     }else{ 
      mapView.region = MKCoordinateRegionMake(CLLocationCoordinate2D(latitude: 52.05695, longitude: 14.50575), MKCoordinateSpan(latitudeDelta: 0.002, longitudeDelta: 0.002)) 
     } 

     let defaultAnnotation = MKPointAnnotation() 
     defaultAnnotation.coordinate = mapView.region.center 
     defaultAnnotation.title="Center" 
     defaultAnnotation.subtitle="This is!" 
     mapView.addAnnotation(defaultAnnotation) 

     mapView.showsUserLocation=true 
     mapView.showsBuildings=true 
     mapView.userTrackingMode = .None 
    } 
} 
+0

是的,那精美的作品。謝謝你,先生。 – brumbrum

相關問題