美文网首页
iOS检测相机、麦克风是否授权

iOS检测相机、麦克风是否授权

作者: Jessica124 | 来源:发表于2017-05-10 11:33 被阅读68次

    .h 文件

    #import <Foundation/Foundation.h>
    
    @interface AuthManager : NSObject
    
    /**
     检测麦克风是否授权
    
     @param callBack 是否授权(如果未授权会有弹出框提示是否去设置,如果不去设置返回否)
     */
    + (void)checkMicphoneAuthCallBack:(void(^)(BOOL flag))callBack;
    
    /**
     检测相机是否授权
     
     @param callBack 是否授权(如果未授权会有弹出框提示是否去设置,如果不去设置返回否)
     */
    + (void)checkCameraAuthCallBack:(void(^)(BOOL flag))callBack;
    
    @end
    

    .m文件

    #import "AuthManager.h"
    #import <AVFoundation/AVFoundation.h>
    #import <UIKit/UIKit.h>
    
    @implementation AuthManager
    
    + (void)checkCameraAuthCallBack:(void (^)(BOOL))callBack{
        [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
    
            if (!granted) {
                NSLog(@"camera未授权");
                NSString *str = @"相机未授权,是否进行授权操作?";
                [self showAlert:str CallBack:^(BOOL cancel) {
                    callBack(cancel);
                }];
            }else{
                NSLog(@"camera已授权");
                callBack(granted);
            }
        }];
    }
    
    + (void)checkMicphoneAuthCallBack:(void (^)(BOOL))callBack {
        [AVCaptureDevice requestAccessForMediaType:AVMediaTypeAudio completionHandler:^(BOOL granted) {
            if (!granted) {
                NSLog(@"mic未授权");
                NSString *str = @"麦克风未授权,是否进行授权操作?";
                [self showAlert:str CallBack:^(BOOL cancel) {
                    callBack(cancel);
                }];
            }else{
                NSLog(@"mic已授权");
                callBack(granted);
            }
        }];
    }
    
    /**
     alert提示
     */
    + (void)showAlert:(NSString *)message CallBack:(void (^)(BOOL))callBack {
        UIAlertController *alertvc = [UIAlertController alertControllerWithTitle:@"温馨提示"
                                                                         message:message
                                                                  preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"否" style:UIAlertActionStyleDefault
                                                       handler:^(UIAlertAction * _Nonnull action) {
            callBack(NO);
        }];
        UIAlertAction *setting = [UIAlertAction actionWithTitle:@"是" style:UIAlertActionStyleDefault
                                                        handler:^(UIAlertAction * _Nonnull action) {
            [self gotoSetting];
        }];
        [alertvc addAction:cancel];
        [alertvc addAction:setting];
        [TOPCONTROLLER presentViewController:alertvc animated:YES completion:nil];
    }
    
    /**
     跳转到设置页面
     */
    + (void)gotoSetting {
        if([[UIApplication sharedApplication]
            canOpenURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]]) {
            [[UIApplication sharedApplication]
             openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
        };
    }
    
    @end
    

    相关文章

      网友评论

          本文标题:iOS检测相机、麦克风是否授权

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