.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