0
点赞
收藏
分享

微信扫一扫

iOS小技能:授权检测(引导权限开启,监听权限变化执行回调事件。)

慕容冲_a4b8 2022-08-31 阅读 155


前言

需求:

  1. 新增开启相册权限引导:在iPhone的"设置-隐私-照片"中允许访问照片

监听到用户点击不允许: 用户未作出明确选择的情况下自己主动请求了一次权限设置

  1. 新增开启相机权限引导:在iPhone的"设置-隐私-相机"中允许访问相机
  2. 新增开启定位权限引导:请在iPhone的"设置-隐私-定位"中允许访问地理位置

用户尚未对该应用程序作出选择,安装之后第一次使用定位时,唤起系统授权页面,并监听权限变化执行回调事件。

iOS小技能:授权检测(引导权限开启,监听权限变化执行回调事件。)_ios

I 授权检测

1.1 定位权限

查看CLLocationManager的授权状态:​​ [CLLocationManager authorizationStatus​

//用户尚未对该应用程序作出选择
kCLAuthorizationStatusRestricted //应用程序的定位权限被限制
kCLAuthorizationStatusAuthorizedAlways //一直允许获取定位
kCLAuthorizationStatusAuthorizedWhenInUse //在使用时允许获取定位
kCLAuthorizationStatusAuthorized //已废弃,相当于一直允许获取定位
kCLAuthorizationStatusDenied //拒绝获取定位

引导权限开启,监听权限变化执行回调事件

/**
showAlert: 是否弹窗引导
block: 回调
*/
+(BOOL)isHasLocationAuthorityWithisShowAlert:(BOOL)showAlert block:(void (^)(id sender))block {


CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
//应用程序的定位权限被限制
//拒绝获取定位
if (status == kCLAuthorizationStatusRestricted || status == kCLAuthorizationStatusDenied) {
NSLog(@"NSLog 没有获取地理位置的权限");
if (showAlert) {
[LBAlertController showAlertTitle:@"无法使用定位" content:@"请在iPhone的\"设置-隐私-定位\"中允许访问地理位置。" cancelString:@"取消" cancleBlock:nil sureString:@"去设置" sureBlock:^{

// 需要在info.plist中添加 URL types 并设置一项URL Schemes为prefs IOS10 以后不起作用
if([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]]){
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
}
} currentController:[QCT_Common getCurrentVC]];
}
return NO;

}else if (status == kCLAuthorizationStatusNotDetermined){//用户尚未对该应用程序作出选择,安装之后第一次使用
CLLocationManager *manager = ERPLBS.shareERPLBS.locationMan;
ERPLBS.shareERPLBS.block4location = block;// 监听状态变化时,执行的block
[manager requestAlwaysAuthorization];
return NO;

}


if(block){// 3. 执行允许之后的定位操作
block(nil);
}
return YES;


}

监听权限变化执行回调事件

- (CLLocationManager *)locationMan{

if(_locationMan == nil){
_locationMan = [CLLocationManager new];

_locationMan.delegate = self;

}
return _locationMan;

}
#pragma

- (void)locationManagerDidChangeAuthorization:(CLLocationManager *)manager API_AVAILABLE(ios(14.0), macos(11.0), watchos(7.0), tvos(14.0)){

CLAuthorizationStatus status = [CLLocationManager authorizationStatus];

NSLog(@"locationManagerDidChangeAuthorization:%d",status);


if(status ==kCLAuthorizationStatusAuthorizedAlways|| status == kCLAuthorizationStatusAuthorizedWhenInUse){// 3 ||4

if(self.block4location){
self.block4location(nil);

}

}



}

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status API_DEPRECATED_WITH_REPLACEMENT("-locationManagerDidChangeAuthorization:", ios(4.2, 14.0), macos(10.7, 11.0), watchos(1.0, 7.0), tvos(9.0, 14.0)){


// nsl
//kCLAuthorizationStatusAuthorizedAlways
// kCLAuthorizationStatusAuthorizedWhenInUse

if(status ==kCLAuthorizationStatusAuthorizedAlways|| status == kCLAuthorizationStatusAuthorizedWhenInUse){//3 ||4

if(self.block4location){
self.block4location(nil);

}

}

}

1.2 访问图库权限检测以及引导

监听到用户点击不允许: 用户未作出明确选择的情况下自己主动请求了一次权限设置

去设置相机权限的的时候系统会kill 当前app进程 ​​Message from debugger: Terminated due to signal 9​

/**

监听到用户点击不允许:
用户未作出明确选择的情况下自己主动请求了一次权限设置


showAlert:不允许时显示引导

block: 允许之后的动作,比如保存图片

*/
+(BOOL)isHasPhotoLibraryAuthorityWithisShowAlert:(BOOL)showAlert block:(void (^)(id sender))block
{

PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus] ;

//1. 定义局部block: 处理没有权限的情况,显示引导
BOOL (^block4none)(PHAuthorizationStatus ) = ^ BOOL (PHAuthorizationStatus status ){

NSLog(@" 没有访问图库的权限==============");

if (showAlert) {

[LBAlertController showAlertTitle:@"无法使用相册" content:@"请在iPhone的\"设置-隐私-照片\"中允许访问照片。" cancelString:@"取消" cancleBlock:nil sureString:@"去设置" sureBlock:^{
// 需要在info.plist中添加 URL types 并设置一项URL Schemes为prefs IOS10 以后不起作用 else的方法

if([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]]){
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
}
} currentController:[QCT_Common getCurrentVC]];
}


return NO;



};


switch (status) {
case PHAuthorizationStatusRestricted:
case PHAuthorizationStatusDenied : {

return block4none(status);


}
break;

case PHAuthorizationStatusNotDetermined:{//2. 用户未作出明确选择的情况下自己主动请求了一次权限设置



[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus phStatus) {

if (phStatus == PHAuthorizationStatusRestricted || phStatus == PHAuthorizationStatusDenied) {


dispatch_sync(dispatch_get_main_queue(), ^{
//刷新UI的代码放到主线程执行
block4none(status);

});



} else if (phStatus == PHAuthorizationStatusNotDetermined) {

// 不处理
} else {
// 执行外围的block
// status = QMUIAssetAuthorizationStatusAuthorized;

if(block){//执行允许之后的保存图片操作
block(nil);
}

}



}];

return NO;

}

default:
break;
}

if(block){// 3. 执行允许之后的保存图片操作
block(nil);
}
return YES;
}

1.3 访问相机的权限检测

/**
@param showAlert 是否弹窗引导
@return 是否有权限
*/
+(BOOL)isHasCameraAuthorityWithisShowAlert:(BOOL)showAlert
{
AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
if (status == AVAuthorizationStatusRestricted || status == AVAuthorizationStatusDenied) {
NSLog(@"LBLog 没有访问相机的权限");
if (showAlert) {
[LBAlertController showAlertTitle:@"无法使用相机" content:@"请在iPhone的\"设置-隐私-相机\"中允许访问相机。" cancelString:@"取消" cancleBlock:nil sureString:@"去设置" sureBlock:^{
// 需要在info.plist中添加 URL types 并设置一项URL Schemes为prefs IOS10 以后不起作用
NSURL *url = [NSURL URLWithString:@"prefs:root=Privacy&path=CAMERA"];
if ([[UIApplication sharedApplication] canOpenURL:url]) {
[[UIApplication sharedApplication] openURL:url];
}else if([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]]){
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
}
} currentController:[DY_Common getCurrentVC]];
return NO;
}else if (status == AVAuthorizationStatusNotDetermined){
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
if (granted) {
NSLog(@"LBLog 获取相机权限正常==============");

}else{
NSLog(@"LBLog 获取相机权限不正常==============");
}
}];
}
}
NSLog(@"LBLog 有访问相机的权限 =============");
return YES;
}

1.4 iOS蓝牙状态的处理(蓝牙关闭及未授权的处理)

  • ​​iOS蓝牙状态的处理【蓝牙关闭及未授权的处理】​​

1.5 注意事项

To resolve this issue, please revise your app to provide the associated functionality using public APIs or remove the functionality using the "prefs:root" or "App-Prefs:root" URL scheme.

see also

公众号:iOS逆向

举报

相关推荐

0 条评论