shows user location在iOS8上不能使用地圖定位
在xcode6中 蘋果地圖得定位方法修改了,以前得不能用了
報錯說明:Trying to start MapKit location updates without prompting for location authorization. Must call -[CLLocationManager requestWhenInUseAuthorization] or -[CLLocationManager requestAlwaysAuthorization] first.
解決辦法:
在Supporting Files下得XXX.info.plis裡面添加NSLocationAlwaysUsageDescription 或 NSLocationWhenInUseDescription 兩個String欄位,value可以為空白,也可以設定YES,不過我得問題還是不能解決,最終還是找到得了問題所在,就是info.plist中還需要包含Supported interface orientations 這個Array欄位。然後運行就解決了.
代碼處理:
locationManager = [[CLLocationManager alloc] init]; locationManager.delegate = self; locationManager.desiredAccuracy=kCLLocationAccuracyBest; if([locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) { [locationManager requestWhenInUseAuthorization]; } [locationManager startUpdatingLocation];
另外一處:
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status { if ( ([locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)] && status != kCLAuthorizationStatusNotDetermined && status != kCLAuthorizationStatusAuthorizedWhenInUse) || (![locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)] && status != kCLAuthorizationStatusNotDetermined && status != kCLAuthorizationStatusAuthorized) ) { NSString *message = @"您的手機目前未開啟定位服務,如欲開啟定位服務,請至設定開啟定位服務功能"; UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"無法定位" message:message delegate:nil cancelButtonTitle:@"確定" otherButtonTitles: nil]; [alertView show]; }else { [locationManager startUpdatingLocation]; }}
上面那行是 iOS 8 以上,第二行是 iOS 7 以下, kCLAuthorizationStatusAuthorized 在 iOS 8 完全不能使用。
別的地方:
CLAuthorizationStatus status = [CLLocationManager authorizationStatus]; if (status == kCLAuthorizationStatusAuthorizedWhenInUse || status == kCLAuthorizationStatusAuthorized) { // 開始定位}else { // 提示警告}
代碼修改片段來源於此:http://blog.dreambreakerx.com/tag/ios/