ios 判断有没有相机权限OC代码
可以使用AVCaptureDevice类的方法来判断设备是否有相机权限。以下是一个示例的Objective-C代码:
#import <AVFoundation/AVFoundation.h>
- (BOOL)isCameraPermissionGranted {
AVAuthorizationStatus authorizationStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
if (authorizationStatus == AVAuthorizationStatusAuthorized) {
return YES;
} else if (authorizationStatus == AVAuthorizationStatusNotDetermined) {
__block BOOL isGranted = NO;
dispatch_semaphore_t sema = dispatch_semaphore_create(0);
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
isGranted = granted;
dispatch_semaphore_signal(sema);
}];
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
return isGranted;
} else {
return NO;
}
}
使用上述方法isCameraPermissionGranted可以判断设备是否有相机权限。如果返回值为YES,则表示有相机权限;如果返回值为NO,则表示没有相机权限。当权限状态为AVAuthorizationStatusNotDetermined时,会弹出系统权限弹窗询问用户是否授权相机权限
原文地址: http://www.cveoy.top/t/topic/iiuF 著作权归作者所有。请勿转载和采集!