| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- //
- // RKSysAccess.m
- // YBVideo
- //
- // Created by YB007 on 2020/8/21.
- // Copyright © 2020 cat. All rights reserved.
- //
- #import "RKSysAccess.h"
- @interface RKSysAccess()
- @property(nonatomic,assign)BOOL alertIsShowing;
- @end
- @implementation RKSysAccess
- static RKSysAccess *_access = nil;
- +(instancetype)shareInstance {
- static dispatch_once_t onceToken;
- dispatch_once(&onceToken, ^{
- _access = [[super allocWithZone:NULL]init];
- });
- return _access;
- }
- + (instancetype)allocWithZone:(struct _NSZone *)zone{
- return [self shareInstance];
- }
- -(BOOL)checkMediaAccess {
- AVAuthorizationStatus videoAuthStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
- AVAuthorizationStatus audioAuthStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];
- if (videoAuthStatus == AVAuthorizationStatusAuthorized && audioAuthStatus == AVAuthorizationStatusAuthorized) {
- return YES;
- }
- return NO;
- }
- -(void)requestMediaAccess:(RKAccessBlock)rkAccess{
- AVAuthorizationStatus videoAuthStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
- AVAuthorizationStatus audioAuthStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];
- if (videoAuthStatus == AVAuthorizationStatusNotDetermined && audioAuthStatus == AVAuthorizationStatusNotDetermined) {
- //相机、麦克风首次授权
- [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
- if (granted == YES) {
- [AVCaptureDevice requestAccessForMediaType:AVMediaTypeAudio completionHandler:^(BOOL granted) {
- if (rkAccess) {
- rkAccess(granted);
- }
- }];
- }else {
- if (rkAccess) {
- rkAccess(NO);
- }
- }
- }];
- }else if (videoAuthStatus != AVAuthorizationStatusNotDetermined && audioAuthStatus == AVAuthorizationStatusNotDetermined){
- //麦克风首次授权
- [AVCaptureDevice requestAccessForMediaType:AVMediaTypeAudio completionHandler:^(BOOL granted) {
- if (rkAccess) {
- rkAccess(granted);
- }
- }];
- }else if (videoAuthStatus == AVAuthorizationStatusNotDetermined && audioAuthStatus != AVAuthorizationStatusNotDetermined){
- //相机首次授权
- [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
- if (rkAccess) {
- rkAccess(granted);
- }
- }];
- }else{
- if (rkAccess) {
- rkAccess(NO);
- }
- [self checkAccess:AVMediaTypeVideo];
- [self checkAccess:AVMediaTypeAudio];
- }
-
-
-
- // if (audioAuthStatus == AVAuthorizationStatusNotDetermined) {
- // [AVCaptureDevice requestAccessForMediaType:AVMediaTypeAudio completionHandler:^(BOOL granted) {
- // if (rkAccess) {
- // rkAccess(granted);
- // }
- // if (granted == NO) {
- // dispatch_async(dispatch_get_main_queue(), ^{
- // [[YBBaseAppDelegate sharedAppDelegate].topViewController.navigationController popViewControllerAnimated:YES];
- // });
- // }
- // }];
- // }
- // if (videoAuthStatus == AVAuthorizationStatusAuthorized && audioAuthStatus == AVAuthorizationStatusAuthorized ) {
- // //都同意
- // }else{
- // [self checkAccess:AVMediaTypeVideo];
- // [self checkAccess:AVMediaTypeAudio];
- // }
- }
- -(void)checkAccess:(AVMediaType)mediaType{
- if (_alertIsShowing) {
- return;
- }
- AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:mediaType];
- switch (authStatus) {
- case AVAuthorizationStatusRestricted: case AVAuthorizationStatusDenied:{
- //被拒或者受限
- _alertIsShowing = YES;
- NSDictionary *alerDic = @{@"title":YZMsg(@"提示"),@"msg":YZMsg(@"麦克风或相机权限未开启,请前往设置"),@"left":YZMsg(@"取消"),@"right":YZMsg(@"设置"),@"richImg":@""};
- [YBAlertView showAlertView:alerDic complete:^(int eventType) {
- _alertIsShowing = NO;
- if (eventType == 1) {
- [self goSet];
- }else{
- [[YBBaseAppDelegate sharedAppDelegate].topViewController.navigationController popViewControllerAnimated:YES];
- }
- }];
- }break;
-
- default:
- break;
- }
- }
- -(void)goSet{
- CGFloat systemVersion = [PublicObj getSysVersion];
- if (systemVersion >= 8.0 && systemVersion < 10.0) {
- NSURL * url = [NSURL URLWithString:@"prefs:root=General&path=Reset"];
- if ([[UIApplication sharedApplication] canOpenURL:url]) {
- [[UIApplication sharedApplication] openURL:url];
- }
- }else if (systemVersion >= 10.0) {
- NSURL * url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
- if ([[UIApplication sharedApplication] canOpenURL:url]) {
- if (@available(iOS 10.0, *)) {
- [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:^(BOOL success) {
- }];
- }
- }
- }
- }
- @end
|