iOS8定位問題解決方案
1、修改info
新增Key: NSLocationAlwaysUsageDescription 和 NSLocationWhenInUseUsageDescription ,這兩個Key的值將分別用於描述應用程式始終使用和使用期間使用定位的說明,這些說明將顯示在使用者佈建中。
info新增索引值對如下 :
應用程式說明參見:
2、CLLocationManager初始化
iOS8CLLocationManager新增執行個體方法 requestWhenInUseAuthorization和requestAlwaysAuthorization,需要在初始化時根據需要調用。
if([CLLocationManager locationServicesEnabled]){ self.locationManage = [[CLLocationManager alloc] init]; self.locationManage.delegate = self; //定位頻率,每個多少米定位一次 self.locationManage.distanceFilter = 200; //設定定位精度 self.locationManage.desiredAccuracy = kCLLocationAccuracyBestForNavigation;//kCLLocationAccuracyBest; //啟動跟蹤定位 [self.locationManage startUpdatingLacation]; if (SYSTEM_VERSION >= 8.0) { //使用期間 [self.locationManage requestWhenInUseAuthorization]; //始終 //or [self.locationManage requestAlwaysAuthorization] } }
3、代理( CLLocationManagerDelegate )
//定位失敗
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
[MBHUDUntil hideAllHUDForView:self.window];
if (![SharedData shareInstance].isMB == NO) {
[MBHUDUntil showHUDToWindowWithText:@"地圖定位失敗,請確認您已允許本程式開啟定位服務"];
}
}
// 跟蹤定位代理方法,每次位置發生變化即會執行(只要定位到相應位置)
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
//如果不需要即時定位,使用完即使關閉定位服務
[_locationManager stopUpdatingLocation];
}
新增下面的代理方法:
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{ switch (status) { case kCLAuthorizationStatusNotDetermined: if ([self.locationManage respondsToSelector:@selector(requestAlwaysAuthorization)]) { [self.locationManage requestWhenInUseAuthorization]; } break; default: break; }}