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