我在我的应用程序中使用相机,我想请求所有其他应用程序提示的流行许可。我怎样才能得到它?
AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
if(authStatus == AVAuthorizationStatusAuthorized) {
// do your logic
authorized = YES;
[self actionLaunchAppCamera];
} else if(authStatus == AVAuthorizationStatusDenied){
// denied
authorized = NO;
} else if(authStatus == AVAuthorizationStatusRestricted){
// restricted, normally won't happen
} else if(authStatus == AVAuthorizationStatusNotDetermined){
// not determined?!
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
if(granted){
NSLog(@"Granted access to %@", AVMediaTypeVideo);
} else {
NSLog(@"Not granted access to %@", AVMediaTypeVideo);
}
}];
} else {
// impossible, unknown authorization status
}
我正在使用上面的代码来检查授权,但它总是进入 authorizedstatus 条件,因为它已经具有权限。我已经从隐私设置中检查了权限是否已经开启。但我从来没有征求过用户的许可。
谢谢大家
最佳答案
编辑:哦,我明白你的意思了。然后你需要创建 AVCaptureDeviceInput 来查看之前是否请求过权限。如果没有,这将提示用户获得许可。
NSError outError = NULL;
AVCaptureDevice captureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
If (captureDevice != NULL) {
AVCaptureDeviceInput captureInput = [AVCaptureDeviceInput deviceInputfromDevice:captureDevice error:outError];
If (captureInput != NULL && outError == NULL) {
// we have permission to use the camera
}
}
您无需卸载应用程序即可重置访问权限。假设您的设备运行的是 iOS 8,只需转到相机设置屏幕并禁用访问:
- 退出应用
- 转到设置 -> 隐私 -> 相机
- 寻找您的应用以关闭开关
- 运行应用
关于ios - 我的应用程序默认有摄像头访问权限。我如何关闭它并请求权限?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30725594/