我正在做一个带有启动相机按钮的 ios 应用程序。
如果设备有可用的相机,我想启用/禁用按钮。
我想检测设备是否有摄像头,以及设备是否有摄像头但受到限制(使用this),因此您无法使用它。
如何检测这两个选项?
谢谢
我正在做一个带有启动相机按钮的 ios 应用程序。
如果设备有可用的相机,我想启用/禁用按钮。
我想检测设备是否有摄像头,以及设备是否有摄像头但受到限制(使用this),因此您无法使用它。
如何检测这两个选项?
谢谢
要检查应用程序中的相机权限状态,请使用以下代码段。
@import AVFoundation;
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
if(status == AVAuthorizationStatusAuthorized) {
// authorized
} else if(status == AVAuthorizationStatusDenied){
// denied
} else if(status == AVAuthorizationStatusRestricted){
// restricted
} else if(status == AVAuthorizationStatusNotDetermined){
// not determined
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
if(granted){
NSLog(@"Granted access");
} else {
NSLog(@"Not granted access");
}
}];
}
}
检查相机是否受限AVAuthorizationStatus
是不够的。如文档中所述:
这种状态通常是不可见的——用于发现设备的 AVCaptureDevice 类方法不会返回用户被限制访问的设备。
因此,为了正确检查,您需要创建一些捕获设备,例如,就像我所做的那样:
AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
if (authStatus == AVAuthorizationStatusAuthorized) {
BOOL atLeastOne = NO;
NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
for (AVCaptureDevice *device in devices) {
if (device) {
atLeastOne = YES;
}
}
if (!atLeastOne) {
authStatus = AVAuthorizationStatusRestricted;
}
}
用户第一次尝试在 ios 6 上使用相机时,系统会自动询问他/她的权限。您不必添加额外的代码(在授权状态为 ALAuthorizationStatusNotDetermined 之前)。
因此,如果用户第一次拒绝,您将无法再次询问。
您可以使用ALAssetsLibrary来检查这一点。检查此解决方案的答案: ask-permission-to-access-camera
希望它可以帮助你。
斯威夫特 3
要决定是否应该启用(或隐藏)相机按钮,您应该检查:
if UIImagePickerController.isSourceTypeAvailable(.Camera){ }
但是然后我会检查用户是否允许访问相机,正如苹果在他们的 PhotoPicker 示例(PhotoPicker 示例 Objective-C)中所建议的那样:
*请注意您必须导入 AVFoundation
let authStatus = AVCaptureDevice.authorizationStatus(forMediaType: AVMediaTypeVideo)
if authStatus == AVAuthorizationStatus.denied {
// Denied access to camera
// Explain that we need camera access and how to change it.
let dialog = UIAlertController(title: "Unable to access the Camera", message: "To enable access, go to Settings > Privacy > Camera and turn on Camera access for this app.", preferredStyle: UIAlertControllerStyle.alert)
let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil)
dialog.addAction(okAction)
self.present(dialog, animated:true, completion:nil)
} else if authStatus == AVAuthorizationStatus.notDetermined { // The user has not yet been presented with the option to grant access to the camera hardware.
// Ask for it.
AVCaptureDevice.requestAccess(forMediaType: AVMediaTypeVideo, completionHandler: { (grantd) in
// If access was denied, we do not set the setup error message since access was just denied.
if grantd {
// Allowed access to camera, go ahead and present the UIImagePickerController.
self.showImagePickerForSourceType(sourceType: UIImagePickerControllerSourceType.camera)
}
})
} else {
// Allowed access to camera, go ahead and present the UIImagePickerController.
self.showImagePickerForSourceType(sourceType: UIImagePickerControllerSourceType.camera)
}
func showImagePickerForSourceType(sourceType: UIImagePickerControllerSourceType) {
let myPickerController = UIImagePickerController()
myPickerController.delegate = self;
myPickerController.sourceType = sourceType
self.present(myPickerController, animated: true, completion: nil)
}
如其他地方所述AVAuthorizationStatus
,尽管枚举中存在“受限”值,但检查它实际上并不会告诉您它是否受到限制。相反,我发现检查源是否启用是有帮助的:
BOOL isCameraAvailable = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];
如果isCameraAvailable
是,NO
那么用户很可能在限制中禁用了相机。请参阅检测 iPhone 应用程序中是否存在相机?