iOS 相机相册授权

2019-12-20  本文已影响0人  woo_5857

//是否有相机权限

+ (BOOL)isCameraPermissions:(UIView*)view {

    // 1、 获取摄像设备

    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

    if(device) {

        // 判断授权状态

        AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];

        if (authStatus == AVAuthorizationStatusRestricted) {

           DLog(@"因为系统原因, 无法访问相机");

            returnNO;

        }elseif(authStatus ==AVAuthorizationStatusDenied) {// 用户拒绝当前应用访问相机

            //无权限

            if(view) {

                [QKAlertManagershowWithSuperView:viewtitle:@"无法使用拍照"message:@"请在手机的“设置>鲜声”中,打开拍照开关"cancelTitle:@"取消"verifyTitle:@"去设置"verifyAction:^(JKAlertAction *action) {

                    NSURL * url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];

                    if([[UIApplicationsharedApplication]openURL:url]) {

                       [[UIApplicationsharedApplication]openURL:url];

                    }

                }configBeforeShow:^(JKAlertView*alertView) {}];

            }

            returnNO;

        }elseif(authStatus ==AVAuthorizationStatusAuthorized) {// 用户允许当前应用访问相机

        }else if (authStatus == AVAuthorizationStatusNotDetermined) { // 用户还没有做出选择

        }

    }else{

        //未检测到您的摄像头, 请在真机上测试

    }

    return YES;

}

//相机授权

+ (void)cameraPermissions {

    // 弹框请求用户授权

   [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {

       if(granted) {

           // 用户接受

       }

   }];

}

//是否有相册授权

+ (BOOL)isPhotoAlbumPermissions:(UIView*)view {

    // 判断授权状态

    PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];

    if (status == PHAuthorizationStatusRestricted) { // 此应用程序没有被授权访问的照片数据。可能是家长控制权限。

        DLog(@"因为系统原因, 无法访问相册");

    }else if (status == PHAuthorizationStatusDenied) { // 用户拒绝访问相册

                //无权限

        if(view) {

            [QKAlertManagershowWithSuperView:viewtitle:@"无法使用相册"message:@"请在手机的“设置>鲜声”中,打开相册开关"cancelTitle:@"取消"verifyTitle:@"去设置"verifyAction:^(JKAlertAction *action) {

                NSURL * url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];

                if([[UIApplicationsharedApplication]openURL:url]) {

                   [[UIApplicationsharedApplication]openURL:url];

                }

            }configBeforeShow:^(JKAlertView*alertView) {}];

        }

        returnNO;

    }else if (status == PHAuthorizationStatusAuthorized) { // 用户允许访问相册

    }else if (status == PHAuthorizationStatusNotDetermined) { // 用户还没有做出选择

    }

    return YES;

}

//相册授权

- (void)photoAlbumPermissions {

    // 弹框请求用户授权

    [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {

        if (status == PHAuthorizationStatusAuthorized) { // 用户点击了好

          // 放一些使用相册的代码

        }

    }];

}

//是否有录音权限

+ (BOOL)isCaptureDevicePermissions:(UIView*)view {

    AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];

    if (authStatus != AVAuthorizationStatusAuthorized) { // 没有开启麦克风

       [QKAlertManagershowWithSuperView:viewtitle:@"提示"message:@"请在系统设置中开启麦克风服务(设置>鲜声>麦克风>开启)"cancelTitle:@"取消"verifyTitle:@"去设置"verifyAction:^(JKAlertAction *action) {

           NSURL * url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];

           if([[UIApplicationsharedApplication]openURL:url]) {

              [[UIApplicationsharedApplication]openURL:url];

           }

       }configBeforeShow:^(JKAlertView*alertView) {}];

       returnNO;

    }

    return YES;

}

//录音授权弹框

+ (void)captureDevicePermissions {

    AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];

    //这里是关键 !!!

    if (authStatus == AVAuthorizationStatusNotDetermined) {

       AVAudioSession *avSession = [AVAudioSession sharedInstance];

       if([avSessionrespondsToSelector:@selector(requestRecordPermission:)]) {

           [avSessionrequestRecordPermission:^(BOOLavailable) {

           }];

       }

    }

}

上一篇下一篇

猜你喜欢

热点阅读