iOS原生类获取定位授权是通过**CoreLocation**框架中的**CLLocationManager**类完成的,这其中可以解决两大类场景的授权问题:
1、原生开发的授权
2、网页混合开发的授权。使用webview加载网页,授权归入APP授权;在浏览器中加载页面,授权归入浏览器授权。

一、概述获取权限过程
1、调用authorizationStatus,获取状态,只要不是允许状态,进行下个步骤
2、CLLocationManager创建实例,并设置代理
3、保持CLLocationManager实例的生命周期到任务结束
4、如果授权状态不是允许,则获取授权或者跳转设置页面继续修改
5、根据需要开启不同的定位服务

二、获取授权状态

//次方法返回值为CLAuthorizationStatus的枚举类型,如下所示
[CLLocationManager authorizationStatus];
typedef NS_ENUM(int, CLAuthorizationStatus) {
    //未做选择:未选允许、拒绝(例如弹出提示框,用户之间退出APP时会发生)
    kCLAuthorizationStatusNotDetermined = 0,
    //受限制:未授权,主动现在定位服务,用户无法改变该状态(可能没有否认个人授权)
    kCLAuthorizationStatusRestricted,
    // 拒绝:用户拒绝授权,可在设置在修改
    kCLAuthorizationStatusDenied,
    //授权且在未使用APP时使用定位
    kCLAuthorizationStatusAuthorizedAlways NS_ENUM_AVAILABLE(NA, 8_0),
    // 授权且当APP使用中使用定位
    kCLAuthorizationStatusAuthorizedWhenInUse NS_ENUM_AVAILABLE(NA, 8_0),
    // 已废弃:不做解释,在官方demo中也不在关注该值
    kCLAuthorizationStatusAuthorized NS_ENUM_DEPRECATED(10_6, NA, 2_0, 8_0, "Use kCLAuthorizationStatusAuthorizedAlways") __TVOS_PROHIBITED __WATCHOS_PROHIBITED = kCLAuthorizationStatusAuthorizedAlways
};

根据当前授权状态做响应的交互处理:

#pragma mark - 检查授权状态
- (void)checkLocationServicesAuthorizationStatus {
    [self reportLocationServicesAuthorizationStatus:[CLLocationManager authorizationStatus]];
}
- (void)reportLocationServicesAuthorizationStatus:(CLAuthorizationStatus)status
{
    if(status == kCLAuthorizationStatusNotDetermined)
    {
        //未决定,继续请求授权
        [self requestLocationServicesAuthorization];
    }
    else if(status == kCLAuthorizationStatusRestricted)
    {
        //受限制,尝试提示然后进入设置页面进行处理(根据API说明一般不会返回该值)
        [self alertViewWithMessage];
    }
    else if(status == kCLAuthorizationStatusDenied)
    {
        //拒绝使用,提示是否进入设置页面进行修改
        [self alertViewWithMessage];
    }
    else if(status == kCLAuthorizationStatusAuthorizedWhenInUse)
    {
        //授权使用,不做处理
    }
    else if(status == kCLAuthorizationStatusAuthorizedAlways)
    {
       //始终使用,不做处理
    }
}
#pragma mark - Helper methods
- (void)alertViewWithMessage {
    UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"定位服务未开启" message:@"请在系统设置中开启服务" delegate:self cancelButtonTitle:@"暂不" otherButtonTitles:@"去设置", nil];
    [alter show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0)
    {
    }
    else
    {
        //进入系统设置页面,APP本身的权限管理页面
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
    }
}

三、获取授权

#pragma mark - 获取授权
- (void)requestLocationServicesAuthorization
{
    //CLLocationManager的实例对象一定要保持生命周期的存活
    if (!self.locationManager) {
        self.locationManager  = [[CLLocationManager alloc] init];
        self.locationManager.delegate = self;
    }
    [self.locationManager requestWhenInUseAuthorization];
    [self.locationManager startUpdatingLocation];
}
#pragma mark - CLLocationMangerDelegate methods
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
    [self.locationManager stopUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    [self.locationManager stopUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
    [self reportLocationServicesAuthorizationStatus:status];
}

关于以上代码几点说明
1、CLLocationManager的对象实例一定要保证生命周期存在知道任务结束,因为定位服务的任务执行时异步,使用局部变量不能保证定位服务正常进行。
2、iOS8系统之后一定要在info.plist文件中添加NSLocationWhenInUseUsageDescription或者NSLocationAlwaysUsageDescription,根据需要选择其一或者其二(决定了授权设置页面中选项数目),内容选填。
3、requestWhenInUseAuthorization或者requestAlwaysAuthorization方法,要在使用定位服务(比如startUpdatingLocation)之前调用。
4、APP进入后台运行的时候,开启定位服务会失败。
5、当进行授权选择后,会调用didChangeAuthorizationStatus方法,此时重新走一次检测授权状态过程。

四、调用方法开始整个获取权限流程

- (void)viewDidLoad {
    [super viewDidLoad];
    [self checkLocationServicesAuthorizationStatus];
}

只需要调用次方法即可。

五、最后的话
定位服务还有其他内容,学习了其他内容会继续更新,详情请参考官方文档,关于其他系统服务权限的获取,可以参考另一篇文章官方Demo

你可能感兴趣的内容
0条评论
EC

ecarrizo

这家伙太懒了,什么都没留下
Owner