转载_iOS获取定位权限/调用系统定位(Objective-C)

2018-03-11  本文已影响0人  Raywf

2018.3.11
如原作者要求删除,请联系我,立删。
原作者:牧晓逸风
链接:https://www.jianshu.com/p/ef6994767cbb
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

1.iOS8以前使用CLLocationManager

// 1.设置位置管理者属性
@property (nonatomic, strong) CLLocationManager *lcManager;
// 2.判断是否打开了位置服务
if ([CLLocationManager locationServicesEnabled]) {
    // 创建位置管理者对象
    self.lcManager = [[CLLocationManager alloc] init];
    self.lcManager.delegate = self; // 设置代理
    // 设置定位距离过滤参数 (当本次定位和上次定位之间的距离大于或等于这个值时,调用代理方法)
    self.lcManager.distanceFilter = 100;
    self.lcManager.desiredAccuracy = kCLLocationAccuracyBest; // 设置定位精度(精度越高越耗电)
    [self.lcManager startUpdatingLocation]; // 开始更新位置
}
/** 获取到新的位置信息时调用*/
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    NSLog(@"定位到了");
}
/** 不能获取位置信息时调用*/
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
    NSLog(@"获取定位失败");
}

Info.plist 设置Key 勾选后台模式

2.iOS8.0之后使用CLLocationManager

1.requestWhenInUseAuthorization
Info.plist 设置对应的Key
[_lcManager requestWhenInUseAuthorization];

请求定位的弹框 后台模式下的 blue bar
2.requestAlwaysAuthorization
Info.plist中 设置对应的Key 请求定位的弹框
3.注意
4.判断是否开启了定位服务
if ([CLLocationManager locationServicesEnabled]) { // 判断是否打开了位置服务
        [self.lcManager startUpdatingLocation]; // 开始更新位置
    }

5.适配版本号的方法
if ([[UIDevice currentDevice].systemVersion floatValue] >=8.0 ) {
[_lcManager requestAlwaysAuthorization];
}

if ([_lcManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
            [_lcManager requestWhenInUseAuthorization];
        }

6.监听定位服务状态的改变
/** 定位服务状态改变时调用*/
-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
    switch (status) {
        case kCLAuthorizationStatusNotDetermined:
        {
            NSLog(@"用户还未决定授权");
            break;
        }
        case kCLAuthorizationStatusRestricted:
        {
            NSLog(@"访问受限");
            break;
        }
        case kCLAuthorizationStatusDenied:
        {
            // 类方法,判断是否开启定位服务
            if ([CLLocationManager locationServicesEnabled]) {
                NSLog(@"定位服务开启,被拒绝");
            } else {
                NSLog(@"定位服务关闭,不可用");
            }
            break;
        }
        case kCLAuthorizationStatusAuthorizedAlways:
        {
            NSLog(@"获得前后台授权");
            break;
        }
        case kCLAuthorizationStatusAuthorizedWhenInUse:
        {
            NSLog(@"获得前台授权");
            break;
        }
        default:
            break;
    }
}

7.代理方法返回的 locations 信息
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    CLLocation *location = [locations firstObject];
    NSLog(@"%@", location);
}

3.iOS9.0之后使用CLLocationManager

    if ([CLLocationManager locationServicesEnabled]) { // 判断是否打开了位置服务
        [self.lcManager requestLocation];
    }

if ([[UIDevice currentDevice].systemVersion floatValue] >=8.0 ) {
     // iOS0.0:如果当前的授权状态是使用是授权,那么App退到后台后,将不能获取用户位置,即使勾选后台模式:location
    [_lcManager requestWhenInUseAuthorization];
    }

// iOS9.0+ 要想继续获取位置,需要使用以下属性进行设置(注意勾选后台模式:location)但会出现蓝条
if ([_lcManager respondsToSelector:@selector(allowsBackgroundLocationUpdates)]) {
    _lcManager.allowsBackgroundLocationUpdates = YES;
}
上一篇 下一篇

猜你喜欢

热点阅读