携手创造,一起成长!这是我参与「日新方案 8 月更文应战」的第7天,点击检查活动详情
前语
需求:
- 新增敞开相册权限引导:在iPhone的”设置-隐私-相片”中答应拜访相片
监听到用户点击不答应: 用户未作出清晰挑选的状况下自己主动恳求了一次权限设置
- 新增敞开相机权限引导:在iPhone的”设置-隐私-相机”中答应拜访相机
- 新增敞开定位权限引导:请在iPhone的”设置-隐私-定位”中答应拜访地理位置
用户没有对该应用程序作出挑选,安装之后第一次运用定位时,引发体系授权页面,并监听权限改变履行回调事情。
I 授权检测
1.1 定位权限
检查CLLocationManager的授权状况: [CLLocationManager authorizationStatus
kCLAuthorizationStatusNotDetermined //用户没有对该应用程序作出挑选
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 mark - ******** CLLocationManagerDelegate
- (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逆向