2019-06-10

#import <Photos/Photos.h>

//是否有相机权限

+ (void)requestAblumAuthorityWithCompletionHandler:(void(^)(NSError*))handler{

    PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];

    if (PHAuthorizationStatusAuthorized == status)

    { if(handler) {

            handler(nil);  }}

    else

    {

        if (PHAuthorizationStatusRestricted == status || PHAuthorizationStatusDenied == status)

        {

            NSString*errMsg =@"此应用需要访问相册,请设置";

            NSDictionary *userInfo = @{NSLocalizedDescriptionKey:errMsg};

            NSError*error = [NSErrorerrorWithDomain:@"访问权限"code:0userInfo:userInfo];

            if(handler) {

                handler(error);

            }

        }


        [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {

            if (status == PHAuthorizationStatusAuthorized)

            {

                if(handler) {

                    handler(nil);

                }

            }

            else

            {

                NSString*errMsg =@"此应用需要访问相册,请设置";

                NSDictionary*userInfo =@{NSLocalizedDescriptionKey:errMsg};

                NSError*error = [NSErrorerrorWithDomain:@"访问权限"code:0userInfo:userInfo];

                if(handler) {

                    handler(error);

                }

            }

        }];

    }

}

+ (BOOL)requestMediaCapturerAccessWithHandler:(void(^)(NSError*))handler {

    AVAuthorizationStatus videoAuthorStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];

    AVAuthorizationStatus audioAuthorStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];


    if (AVAuthorizationStatusAuthorized == videoAuthorStatus && AVAuthorizationStatusAuthorized == audioAuthorStatus) {

        if(handler) {

            handler(nil);

        }

    }else{

        if(AVAuthorizationStatusRestricted== videoAuthorStatus ||AVAuthorizationStatusDenied== videoAuthorStatus) {

            NSString *errMsg = NSLocalizedString(@"此应用需要访问摄像头,请设置", @"此应用需要访问摄像头,请设置");

            NSDictionary *userInfo = @{NSLocalizedDescriptionKey:errMsg};

            NSError*error = [NSErrorerrorWithDomain:@"访问权限"code:0userInfo:userInfo];

            if(handler) {

                handler(error);

            }


            returnNO;

        }


        if(AVAuthorizationStatusRestricted== audioAuthorStatus ||AVAuthorizationStatusDenied== audioAuthorStatus) {

            NSString *errMsg = NSLocalizedString(@"此应用需要访问麦克风,请设置", @"此应用需要访问麦克风,请设置");

            NSDictionary *userInfo = @{NSLocalizedDescriptionKey:errMsg};

            NSError*error = [NSErrorerrorWithDomain:@"访问权限"code:0userInfo:userInfo];

            if(handler) {

                handler(error);

            }


            returnNO;

        }


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

            if(granted) {

                [[AVAudioSession sharedInstance] requestRecordPermission:^(BOOL granted) {

                    if(granted) {

                        if(handler) {

                            handler(nil);

                        }

                    }else{

                        NSString*errMsg =NSLocalizedString(@"不允许访问麦克风",@"不允许访问麦克风");

                        NSDictionary*userInfo =@{NSLocalizedDescriptionKey:errMsg};

                        NSError*error = [NSErrorerrorWithDomain:@"访问权限"code:0userInfo:userInfo];

                        if(handler) {

                            handler(error);

                        }

                    }

                }];

            }else{

                NSString*errMsg =NSLocalizedString(@"不允许访问摄像头",@"不允许访问摄像头");

                NSDictionary*userInfo =@{NSLocalizedDescriptionKey:errMsg};

                NSError*error = [NSErrorerrorWithDomain:@"访问权限"code:0userInfo:userInfo];

                if(handler) {

                    handler(error);

                }

            }

        }];


    }

    return YES;

}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 如果要给一个属性不一样的访问方法,需要在property里进行指定,如下:@property(getter = i...
    nimomeng阅读 2,713评论 0 0
  • 如果要给一个属性不一样的访问方法,需要在property里进行指定,如下: @property(getter = ...
    nimomeng阅读 5,934评论 0 0
  • 腾讯服务器安装环境配置网址 创建时间:2018年12月12日(星期三) 下午2:32|分类:未分类 | 字数:10...
    往事love随缘阅读 936评论 0 0
  • Change Log All notable changes to this project will be do...
    SmallTwo阅读 3,736评论 0 1
  • 异常 异常(exception)就是指必须中断程序的正常执行来进行处理的特殊状态。 编码时采取将异常发生时的处理和...
    陈_振阅读 3,486评论 0 0