美文网首页iOS
iOS 10 系统权限处理逻辑(以麦克风为例)

iOS 10 系统权限处理逻辑(以麦克风为例)

作者: JasonEVA | 来源:发表于2017-01-10 11:58 被阅读5257次

    iOS 10加强的隐私数据保护,需要使用系统权限必须弹框提示用户,同意才能使用,而且要在plist中添加相应的key。

    10之前只需要获取位置时配置,现在更严格了,比如需要调用相册访问权限,也需要在Info.plist中配置privacy。好在这些key的名字在Xcode 8中已经有了自动补全。添加一个属性,输入Privacy后就会出现自动提示:



    后面填的string会在弹出用户允许时展示在描述里。如果描述空着提交AppStore时会拒绝。

    用户在点击允许后一切正常,但是点击不允许之后就需要对权限的状态进行判断,并作出相应的处理。

    以点击录音按钮为例:

    AVAuthorizationStatus videoAuthStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];
                if (videoAuthStatus == AVAuthorizationStatusNotDetermined) {// 未询问用户是否授权
                    //第一次询问用户是否进行授权
                    [[AVAudioSession sharedInstance] requestRecordPermission:^(BOOL granted) {
                        // CALL YOUR METHOD HERE - as this assumes being called only once from user interacting with permission alert!
                        if (granted) {
                            // Microphone enabled code
                        }
                        else {
                            // Microphone disabled code
                        }
                    }];
                }
                else if(videoAuthStatus == AVAuthorizationStatusRestricted || videoAuthStatus == AVAuthorizationStatusDenied) {// 未授权
                    [self showSetAlertView];
                }
                else{// 已授权
                    [self recordVoiceStart];
                }
                
    
    //提示用户进行麦克风使用授权
    - (void)showSetAlertView {
        UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"麦克风权限未开启" message:@"麦克风权限未开启,请进入系统【设置】>【隐私】>【麦克风】中打开开关,开启麦克风功能" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        UIAlertAction *setAction = [UIAlertAction actionWithTitle:@"去设置" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            //跳入当前App设置界面
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
        }];
        [alertVC addAction:cancelAction];
        [alertVC addAction:setAction];
        
        [self presentViewController:alertVC animated:YES completion:nil];
    }
    
    

    下面的方法只会在未询问过用户的情况下生效,也就是只能第一次询问的时候调用,如果第一次调用时点击了不允许,再次监测状态时调用无效,只能提示用户去设置中打开开关。

    [[AVAudioSession sharedInstance] requestRecordPermission:^(BOOL granted) {
                        // CALL YOUR METHOD HERE - as this assumes being called only once from user interacting with permission alert!
                        if (granted) {
                            // Microphone enabled code
                        }
                        else {
                            // Microphone disabled code
                        }
                    }];
    
    

    相关文章

      网友评论

        本文标题:iOS 10 系统权限处理逻辑(以麦克风为例)

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