iOS 定位权限(应用使用中定位、一直定位)

1. 判断定位状态

1.1 定位说明

ios 8.0之前是不需要info.plist中进行描述性文字说明的。可直接根据代码使用

1.2 判断是否开启定位权限(不弹出认证权限)

只是会判断是不是开启定位权限,并不会弹出权限认证框。[CLLocationManager locationServicesEnabled]是判断【设置】-【隐私】-【定位服务】所在开关,如果是打开状态,返回为YES,关闭状态则返回NO

#import  


/**
 定位权限开关
 (需要在info中配置)Privacy - Location When In Use Usage Description 允许**在应用使用期间访问您的位置,来用于**功能
 (需要在info中配置)Privacy - Location Always and When In Use Usage Description 允许**访问您的位置,来用于**功能
 
 @return YES:有权限,NO:没权限
 */
- (BOOL)JX_Device_Permission_LocationAuth {
    if (![CLLocationManager locationServicesEnabled]) {
        return NO;
    }
    CLAuthorizationStatus CLstatus = [CLLocationManager authorizationStatus];
    if (CLstatus == kCLAuthorizationStatusDenied || CLstatus == kCLAuthorizationStatusRestricted) {
        return NO;
    }
    return YES;
}

2.使用中定位服务

2.1 开始定位权限认证弹框(使用中定位)

#import  

@interface JXDeviceHelper ()

/**
 定位回调
 */
@property (nonatomic,copy) void (^JX_Device_Location)(CLAuthorizationStatus state);
@property (nonatomic, strong) CLLocationManager         *locationManager;       // 定位

@end

@implementation JXDeviceHelper

/**
 定位权限开关
 (需要在info中配置)Privacy - Location When In Use Usage Description 允许**在应用使用期间访问您的位置,来用于**功能
 (需要在info中配置)Privacy - Location Always and When In Use Usage Description 允许**访问您的位置,来用于**功能
 
 @return YES:有权限,NO:没权限
 */
- (BOOL)JX_Device_Permission_LocationAuth {
    if (![CLLocationManager locationServicesEnabled]) {
        return NO;
    }
    CLAuthorizationStatus CLstatus = [CLLocationManager authorizationStatus];
    if (CLstatus == kCLAuthorizationStatusDenied || CLstatus == kCLAuthorizationStatusRestricted) {
        return NO;
    }
    return YES;
}

/**
 定位权限开关
 (需要在info中配置)Privacy - Location When In Use Usage Description 允许**访问您的位置,来用于**功能
 */
- (void)JX_Device_Permission_Check_LocationAuth_InUse:(CheckPermissionInUseLocationAuth)permission {
    self.locationManager = [[CLLocationManager alloc] init];
    [self.locationManager requestWhenInUseAuthorization];
    __weak typeof(self) weakSelf = self;
    self.locationManager.delegate    = self;
    [self setJX_Device_Location:^(CLAuthorizationStatus state) {
        dispatch_async(dispatch_get_main_queue(), ^{
            BOOL auth = [weakSelf JX_Device_Permission_LocationAuth];
            // do something
            // 判断认证状态做处理
        });
    }];
}


#pragma mark - 定位回调(CLLocationManagerDelegate)
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{
    if (status == kCLAuthorizationStatusNotDetermined) return; // 因为会多次回调,所以未确认权限不反悔
    if (self.JX_Device_Location) {
        self.JX_Device_Location(status);
    }
}

2.2 定位标志

定位允许成功之后,会在顶部有蓝色状态栏

3.后台定位服务

3.1 开始定位权限认证(后台定位)

#import  

@interface JXDeviceHelper ()

/**
 定位回调
 */
@property (nonatomic,copy) void (^JX_Device_Location)(CLAuthorizationStatus state);
@property (nonatomic, strong) CLLocationManager         *locationManager;       // 定位

@end

@implementation JXDeviceHelper

/**
 定位权限开关
 (需要在info中配置)Privacy - Location When In Use Usage Description 允许**在应用使用期间访问您的位置,来用于**功能
 (需要在info中配置)Privacy - Location Always and When In Use Usage Description 允许**访问您的位置,来用于**功能
 
 @return YES:有权限,NO:没权限
 */
- (BOOL)JX_Device_Permission_LocationAuth {
    if (![CLLocationManager locationServicesEnabled]) {
        return NO;
    }
    CLAuthorizationStatus CLstatus = [CLLocationManager authorizationStatus];
    if (CLstatus == kCLAuthorizationStatusDenied || CLstatus == kCLAuthorizationStatusRestricted) {
        return NO;
    }
    return YES;
}

/**
 定位权限开关
 (需要在info中配置)Privacy - Location Always and When In Use Usage Description 允许**访问您的位置,来用于**功能
 */
- (void)JX_Device_Permission_Check_LocationAuth_Always:(CheckPermissionAlwaysLocationAuth)permission {
    self.locationManager = [[CLLocationManager alloc] init];
    [self.locationManager requestAlwaysAuthorization];
    __weak typeof(self) weakSelf = self;
    self.locationManager.delegate    = self;
    [self setJX_Device_Location:^(CLAuthorizationStatus state) {
        dispatch_async(dispatch_get_main_queue(), ^{
            BOOL auth = [weakSelf JX_Device_Permission_LocationAuth];
            // do something
            // 判断认证状态做处理
        });
    }];
}


#pragma mark - 定位回调(CLLocationManagerDelegate)
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{
    if (status == kCLAuthorizationStatusNotDetermined) return; // 因为会多次回调,所以未确认权限不反悔
    if (self.JX_Device_Location) {
        self.JX_Device_Location(status);
    }
}

3.2 开启后台定位模式

iOS 定位权限(应用使用中定位、一直定位)_第1张图片
开启后台定位模式

3.3 定位标志

当后台定位中,不会有蓝色状态栏出现

4.注意情况

  • iOS 8之后,如果想要定位,应用使用中定位需要使用requestWhenInUseAuthorization 、前后台都要定位需要使用requestAlwaysAuthorization方法。

  • 如果两个请求授权的方法都执行了,会出现以下情况

    1.when-in-use写在前面,第一次打开程序时请求授权,如果勾选了后台模式,进入后台会出现蓝条提示正在定位。当程序退出,第二次打开程序时Always 会再次请求授权。之后进入后台就不会出现蓝色状态栏。
    2.Always写在前面, when-in-use写在后面,只会在第一次打开程序时请求授权,因为Always得到的授权大于when-in-use的到的授权

你可能感兴趣的:(iOS 定位权限(应用使用中定位、一直定位))