2019-09-02

相机、相册权限的研究

最近公司项目要增加设备隐私权限的验证,所以研究了一下相机、相册的权限开启与关闭。
一般情况,在第一次点击用户头像的时候需要判断是否开启了相应权限,此处我们可以根据,Info.plist相应的配置来提醒用户是否允许访问此功能,相册代码如下:

[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) { //弹出访问权限提示框
                        if (status == PHAuthorizationStatusAuthorized) {//弹出提示框-允许
                      
                        }
                    }];
相机如下:
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
                    if (granted) { // 允许
                        
                    }
                }];
如果用户点击了允许则开启相应的权限,否则拒绝开启,就需要在下次点击的时候判断当前的状态然后给相应的提示,让用户选择取开启,相册代码如下:
PHAuthorizationStatus authorizationStatus = [PHPhotoLibrary authorizationStatus];
            if (authorizationStatus == PHAuthorizationStatusAuthorized) {//允许
                UIImagePickerController *imagePickerC = [[UIImagePickerController alloc] init];
                imagePickerC.delegate = self;
                imagePickerC.editing = YES;
                imagePickerC.allowsEditing = YES;
                //                imagePickerC.navigationBar.translucent = NO;
                imagePickerC.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:DEF_MainStyle_COLOR,NSFontAttributeName:[UIFont boldSystemFontOfSize:18]};
                imagePickerC.navigationBar.tintColor = [UIColor whiteColor];
                [self presentViewController:imagePickerC animated:YES completion:nil];
                
            } else {
                if (authorizationStatus == PHAuthorizationStatusNotDetermined) {
                    [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) { //弹出访问权限提示框
                        if (status == PHAuthorizationStatusAuthorized) {//弹出提示框-允许
                            
                            UIImagePickerController *imagePickerC = [[UIImagePickerController alloc] init];
                            imagePickerC.delegate = self;
                            imagePickerC.editing = YES;
                            imagePickerC.allowsEditing = YES;
                            imagePickerC.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName:[UIFont boldSystemFontOfSize:18]};
                            imagePickerC.navigationBar.tintColor = [UIColor whiteColor];
                            [self presentViewController:imagePickerC animated:YES completion:nil];
                            
                        }
                    }];
                } else {
                    [UIAlertView showAlertWithTitle:@"提示" message:@"相册权限被禁用,请到设置中开启相册权限" cancleButtonTitle:@"取消" confirmButtonTitle:@"去设置" cancleBlock:^{
                        
                    } confirmBlcok:^{
                        NSURL *set = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
                        [[UIApplication sharedApplication] openURL:set];
                    }];
                }
            }

相机其实是完全类似的,这里就不贴代码了,最后讲一下4中status
typedef NS_ENUM(NSInteger, PHAuthorizationStatus) {

PHAuthorizationStatusNotDetermined = 0, // 默认还没做出选择

PHAuthorizationStatusRestricted, // 此应用程序没有被授权访问的照片数据

PHAuthorizationStatusDenied, // 用户已经明确否认了这一照片数据的应用程序访问

PHAuthorizationStatusAuthorized // 用户已经授权应用访问照片数据

} NS_AVAILABLE_IOS(8_0);

你可能感兴趣的:(2019-09-02)