1、需要導(dǎo)入頭文件
#import <AVFoundation/AVFoundation.h>
2赡麦、跳轉(zhuǎn)頁(yè)面判斷
-(void)gotoNextPage{
NSString *mediaType = AVMediaTypeVideo;
AVAuthorizationStatus author = [AVCaptureDevice authorizationStatusForMediaType:mediaType];
if (author == AVAuthorizationStatusRestricted ||
author ==AVAuthorizationStatusDenied){
//無(wú)權(quán)限
DLog(@"暫無(wú)權(quán)限");
// [NSToastView nsShowToast:@"你還未打開(kāi)相機(jī)權(quán)限猜憎,要掃碼必須要先去設(shè)置打開(kāi)相機(jī)權(quán)限"];
[self openDeviceSettingPage];
return;
}else if (author == AVAuthorizationStatusNotDetermined){
//獲取權(quán)限 如果這個(gè)時(shí)候沒(méi)有獲取權(quán)限而是去設(shè)置界面在權(quán)限列表可能會(huì)沒(méi)有相機(jī)的權(quán)限
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
dispatch_async(dispatch_get_main_queue(), ^{
@strongify(self)
if (granted) {
//允許訪問(wèn) 如果明確下個(gè)界面可以在這邊直接跳轉(zhuǎn)
}else{
//不允許訪問(wèn)
}
});
}];
return;
}
if (TARGET_IPHONE_SIMULATOR) {
///屏蔽模擬器編譯失敗問(wèn)題
return;
}
////這邊就可以做跳轉(zhuǎn)下個(gè)界面的操作了
}
///去項(xiàng)目的設(shè)置界面開(kāi)啟對(duì)應(yīng)的權(quán)限
-(void)openDeviceSettingPage{
UIAlertController *alertCtrl = [UIAlertController alertControllerWithTitle:@"打開(kāi)攝像頭失敗" message:@"請(qǐng)?jiān)?設(shè)置-隱私-相機(jī) 開(kāi)啟權(quán)限" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
}];
[alertCtrl addAction:cancelAction];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
}];
[alertCtrl addAction:okAction];
dispatch_async(dispatch_get_main_queue(), ^{
///[self currentNavigationController] 可以用你當(dāng)前的控制器替換
[[self currentNavigationController] presentViewController:alertCtrl animated:YES completion:nil];
});
}