新增Key: NSLocationAlwaysUsageDescription 和 NSLocationWhenInUseUsageDescription ,這兩個Key的值將分別用於描述應用程序始終使用和使用期間使用定位的說明,這些說明將顯示在用戶設置中。
info新增鍵值對如下 :
應用程序說明參見微信:
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] } }
//定位失敗
- (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; } }