_instance.locationManager = [[CLLocationManager alloc] init];//创建位置管理器
_instance.locationManager.delegate=_instance;
_instance.locationManager.desiredAccuracy=kCLLocationAccuracyBest;
_instance.locationManager.distanceFilter=100.0f;
_instance.updating=NO;
//定位服务是否可用
BOOL enable=[CLLocationManager locationServicesEnabled];
if ([[[UIDevicecurrentDevice]systemVersion]doubleValue] > 8.0)
{
//是否具有定位权限
int status=[CLLocationManager authorizationStatus];
if(!enable || status<3){
//请求权限
[_instance.locationManager requestWhenInUseAuthorization];
//_instance.locationManager requestAlwaysAuthorization];// 前后台同时定位
}
}
应该在 [locationManager startUpdatingLocation];这一句上面加上判断。
if ([[[UIDevicecurrentDevice]systemVersion]doubleValue] >8.0)
{
[locationManager requestWhenInUseAuthorization];//前台定位
// [locationManager requestAlwaysAuthorization];// 前后台同时定位
}
加上以后打开info.plist。
在最后一行添加NSLocationWhenInUseUsageDescription或者NSLocationAlwaysUsageDescription(根据上面写得前台定位还是前后台定位),键值对可以为空。
如图:
注意IOS9以后定位跟contacts一样都需要判断是否进行隐私授权的,方法跟contacts一样,借鉴contacts一篇。定位也有方法authorizationStatus