美文网首页iOS (Swift & Objective-C & Xcode)
iOS相机、麦克风等权限的判断与设置

iOS相机、麦克风等权限的判断与设置

作者: 梧雨北辰 | 来源:发表于2017-01-09 20:01 被阅读4080次

    一、iOS应用权限检测

    在涉及到这个问题的时候,首先为了适配iOS10系统,我们必须首先在info.plist文件中声明将要用到的权限,否则将会引起崩溃如下:
    “This app has crashed because it attempted to access privacy-sensitive data without a usage description. The app's Info.plist must contain an NSMicrophoneUsageDescription key with a string value explaining to the user how the app uses this data.”
    那么设置权限声明的的方式如下:


    屏幕快照 2017-01-09 下午7.52.07.png

    我们需要点击Info.plist中加号,增加需要授权key值并填写相应的权限使用声明。

    1.相机与麦克风

    检测相机与麦克风权限需要导入AVFoundataion框架

    #import <AVFoundation/AVFoundation.h>
    
    
     /**
    //相机、麦克风的授权状态
    typedef NS_ENUM(NSInteger, AVAuthorizationStatus) {
        AVAuthorizationStatusNotDetermined = 0,//未询问过用户是否授权
        AVAuthorizationStatusRestricted, //未授权,例如家长控制
        AVAuthorizationStatusDenied, //未授权,用户曾选择过拒绝授权
        AVAuthorizationStatusAuthorized //已经授权
    } NS_AVAILABLE_IOS(7_0) __TVOS_PROHIBITED;
    */
    //AVMediaTypeVideo:相机权限
    //AVMediaTypeAudio:麦克风权限
    
    /**
     检测相机的方法
     @param permissionGranted 相机授权成功执行的方法
     @param noPermission 相机授权失败或者未授权执行的方法
     */
    + (void)checkCameraAuthorizationGrand:(void (^)())permissionGranted withNoPermission:(void (^)())noPermission{
        AVAuthorizationStatus videoAuthStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
        switch (videoAuthStatus) {
            case AVAuthorizationStatusNotDetermined:
            {
                //第一次提示用户授权
                [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
                    granted ? permissionGranted() : noPermission();
                }];
                break;
            }
            case AVAuthorizationStatusAuthorized:
            {
                //通过授权
                permissionGranted();
                break;
            }
            case AVAuthorizationStatusRestricted:
                //不能授权
                NSLog(@"不能完成授权,可能开启了访问限制");
            case AVAuthorizationStatusDenied:{
                //提示跳转到相机设置(这里使用了blockits的弹窗方法)
                UIAlertView *alert = [UIAlertView  bk_showAlertViewWithTitle:@"相机授权" message:@"跳转相机授权设置" cancelButtonTitle:@"取消" otherButtonTitles:@[@"设置"] handler:^(UIAlertView *alertView, NSInteger buttonIndex) {
                    if (buttonIndex == 1) {
                        //请求授权
                        [self requetSettingForVideo];
                     }
                 }];
                [alert show];
            }
                break;
            default:
                break;
        }
    }
    
    2.相册

    这里针对于iOS8及其以后的系统相册检测方法,使用到的PHPhotoLibrary需要导入Photos框架。

    #import <Photos/Photos.h>
    
    //相册的授权状态
    typedef NS_ENUM(NSInteger, PHAuthorizationStatus) {
        PHAuthorizationStatusNotDetermined = 0, // User has not yet made a choice with regards to this application
        PHAuthorizationStatusRestricted,        // This application is not authorized to access photo data.
                                                // The user cannot change this application’s status, possibly due to active restrictions
                                                //   such as parental controls being in place.
        PHAuthorizationStatusDenied,            // User has explicitly denied this application access to photos data.
        PHAuthorizationStatusAuthorized         // User has authorized this application to access photos data.
    } PHOTOS_AVAILABLE_IOS_TVOS(8_0, 10_0);
    
    
    /**
     检测访问相册的权限
     这里的方法适用于iOS8及其以后版本
     @param permissionGranted 相册授权成功执行的方法
     @param noPermission 相册授权失败或者未授权执行的方法
     */
    + (void)checkPhotoAlbumAuthorizationGrand:(void (^)())permissionGranted withNoPermission:(void (^)())noPermission{
        PHAuthorizationStatus photoAuthStatus = [PHPhotoLibrary authorizationStatus];
        switch (photoAuthStatus) {
            case PHAuthorizationStatusNotDetermined:
            {
                //第一次提示用户授权
                [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
                    status == PHAuthorizationStatusAuthorized ? permissionGranted() : noPermission();
                }];
                break;
            }
            case PHAuthorizationStatusAuthorized:
            {
                //已经通过授权
                permissionGranted();
                break;
            }
            case PHAuthorizationStatusRestricted:
                //不能授权
                NSLog(@"不能完成授权,可能开启了访问限制");
            case PHAuthorizationStatusDenied:{
                //提示跳转相册授权设置
                UIAlertView *alert = [UIAlertView  bk_showAlertViewWithTitle:@"相册授权" message:@"跳转相册授权设置" cancelButtonTitle:@"取消" otherButtonTitles:@[@"设置"] handler:^(UIAlertView *alertView, NSInteger buttonIndex) {
                    if (buttonIndex == 1) {
                        [self requetSettingForPhotoAlbum];
                    }
                }];
                [alert show];
                break;
            }
            default:
                break;
        }
    }
    

    二、iOS应用跳转权限设置

    在iOS8以后的系统中,跳转设置使用如下方法:

    + (void)requetSettingForAuth{
        NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
        if ([ [UIApplication sharedApplication] canOpenURL:url])
        {   
            [[UIApplication sharedApplication] openURL:url];
        }
    }
    

    三、使用注意

    我们在检测授权的时候弹窗会有授权和不授权的回调,有时候我们会在这里处理一些自定义UI问题,这里一定要在主线程中进行,否则会出现崩溃等问题,回到主线程中的操作如下:

      dispatch_async(dispatch_get_main_queue(), ^{
               //处理UI问题            
    });
    

    相关文章

      网友评论

        本文标题:iOS相机、麦克风等权限的判断与设置

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