第一步:在info.plist文件添加两个字段
NSLocationAlwaysUsageDescription - > YES
第二步:
- (CLLocationManager *)locationManager
{
if (!locationManager) {
locationManager = [[CLLocationManager alloc]init];
locationManager.delegate = (id)self;
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
[locationManager requestAlwaysAuthorization];
locationManager.distanceFilter =kCLDistanceFilterNone;
[locationManager startUpdatingLocation];
}
return locationManager;
}
第三步
/*用户变更了程序的定位服务状态*/
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{
switch (status) {
casekCLAuthorizationStatusNotDetermined:
if ([locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
[locationManager requestWhenInUseAuthorization];
}
break;
default:
break;
}
}