美文网首页
2019-09-02

2019-09-02

作者: 某某cp | 来源:发表于2019-09-29 09:10 被阅读0次

相机、相册权限的研究

最近公司项目要增加设备隐私权限的验证,所以研究了一下相机、相册的权限开启与关闭。
一般情况,在第一次点击用户头像的时候需要判断是否开启了相应权限,此处我们可以根据,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);

相关文章

  • GDAS002-Bioconductor备忘录

    title: GDAS002-Bioconductor备忘录date: 2019-09-02 12:0:00typ...

  • TED-003 重新认识出轨行为

    分类:社会 时间:2019-09-02 片长:21:35 TED简介:幸福的人为什么会出轨?人们说起不忠,真正是指...

  • 2019-09-02 量化技术择时

    2019-09-02 量化技术择时 提醒:所有策略指标只能提供参考,均有失效可能,请谨慎使用,自行承担风险 【必看...

  • 我的星球:狼心狗肺星

    2019-09-02 晴 北京·昌平 狗窝 凌晨村口买夜宵看到站街的妓女有感:如果结婚只是避免劳苦的生活,那婚姻无...

  • 价值的“粒波二象性”与马克思劳动价值论的修正

    林汉扬 640 3 收藏2019-09-02 价值的“波粒二象性” 以前我曾经提到过价值的波粒两象性,昨天...

  • 2019-09-02

    2019-09-02 2019年9月2号: 今天,孩子们开学了,早上儿子早早的醒了,就说妈妈,你快点起来给我做饭吧...

  • 2019-09-02

    《大般涅槃经梵行品第二十之一 》选读 天上天下无如佛 “善男子,菩萨摩诃萨行布施时,于诸众生慈心平等犹如子想。又行...

  • 2019-09-02

    现在最重要的事儿,就是睡个好觉,迎接美好的明天~

  • 2019-09-02

    求数组的随机下标 数组去重

  • 2019-09-02

    感恩日记(419) 《苦读》 莫道读书苦, 那是通往世界的路, 圣贤智慧的结晶,, 滋养灵魂, 破迷雾, 每个字母...

网友评论

      本文标题:2019-09-02

      本文链接:https://www.haomeiwen.com/subject/fwiqyctx.html