請(qǐng)求相機(jī)權(quán)限冯痢,未授權(quán)過(guò)的話彈出系統(tǒng)彈框则奥,不是第一次授權(quán)直接返回結(jié)果,如果被禁止了提示用戶(hù)去開(kāi)啟爆阶,支持8.0以上系統(tǒng)直接跳轉(zhuǎn)到設(shè)置
+ (void)requestUseVideoCamera:(void(^)(BOOL isCanUse))CompletionHandler
{
NSString *tipTextWhenNoPhotosAuthorization; // 提示語(yǔ)
NSString *mediaType = AVMediaTypeVideo; //讀取媒體類(lèi)型
AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:mediaType]; //讀取設(shè)備授權(quán)狀態(tài)
if(authStatus == AVAuthorizationStatusRestricted || authStatus == AVAuthorizationStatusDenied) {
NSDictionary *mainInfoDictionary = [[NSBundle mainBundle] infoDictionary];
NSString *appName = [mainInfoDictionary objectForKey:@"CFBundleDisplayName"];
tipTextWhenNoPhotosAuthorization = [NSString stringWithFormat:@"請(qǐng)?jiān)赲"設(shè)置-隱私-相機(jī)\"選項(xiàng)中窍育,允許%@訪問(wèn)你的手機(jī)相機(jī)", appName];
UIViewController *currentController = [[AppDelegate appDelegate] getNewCurrentViewController];
[self showAlertViewFromController:currentController
title:@"溫馨提示"
message:tipTextWhenNoPhotosAuthorization
CancleButtonTitle:@"取消"
otherButtonTitle:@"去設(shè)置"
cancleButtonClick:^{
} otherButtonClick:^{
[self openSystemSetting];
}];
// 展示提示語(yǔ)
NSLog(@" -- %@ ",tipTextWhenNoPhotosAuthorization);
if (CompletionHandler) {
CompletionHandler(NO);
}
}
else if(authStatus == AVAuthorizationStatusNotDetermined) { //第一次請(qǐng)求遂铡。
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
dispatch_async(dispatch_get_main_queue(), ^{
if (CompletionHandler) {
CompletionHandler(granted);
}
}];
});
}
else {
if (CompletionHandler) {
CompletionHandler(YES);
}
}
}
+ (void)openSystemSetting
{
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]]) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
}
}
+ (void)showAlertViewFromController:(UIViewController *)controller
title:(NSString *)title
message:(NSString *)message
CancleButtonTitle:(NSString *)cancleTitle
otherButtonTitle:(NSString *)otherTitle
cancleButtonClick:(void(^)(void))cancleClick
otherButtonClick:(void(^)(void))otherButtonClick
{
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title
message:message
preferredStyle:UIAlertControllerStyleAlert];
[alertController addAction:[UIAlertAction actionWithTitle:cancleTitle
style:UIAlertActionStyleCancel
handler:^(UIAlertAction * _Nonnull action) {
cancleClick ();
}]];
[alertController addAction:[UIAlertAction actionWithTitle:otherTitle
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * _Nonnull action) {
otherButtonClick ();
}]];
[controller presentViewController:alertController
animated:YES
completion:nil];
}
需要導(dǎo)入
#import <AVFoundation/AVCaptureDevice.h>
其中遇到的比較坑的點(diǎn)是
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
//分線程
});
回調(diào)的block是分線程截亦。不小心在block里操作UI就會(huì)有詭異的現(xiàn)象發(fā)生爬泥,比如push會(huì)大約8秒才跳轉(zhuǎn)。