原文 http://blog.csdn.net/nextstudio/article/details/40050095
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 = ;
//设置定位精度
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;
}
}