如何检测相机是否受用户限制

时间:2013-05-31 11:20:05

标签: ios camera restrictions

我正在使用启动相机的按钮来执行ios应用程序。

如果设备有可用的相机,我想启用/禁用按钮。

我想检测设备是否有摄像头,以及设备是否有摄像头但是受限制(使用this),因此您无法使用它。

如何检测这两个选项?

由于

5 个答案:

答案 0 :(得分:26)

要在应用中检查相机权限状态,请使用以下代码段。

@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");
          }
      }];
   }
}

答案 1 :(得分:4)

检查相机限制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;
    }
}

答案 2 :(得分:3)

用户第一次尝试使用ios 6上的摄像头时,会自动要求他/她进行许可。您不必添加额外的代码(在此之前,authorisationstatus是ALAuthorizationStatusNotDetermined)。

因此,如果用户第一次拒绝你就会拒绝。

您可以使用 ALAssetsLibrary 进行检查。 检查此解决方案的答案: ask-permission-to-access-camera

希望它对你有所帮助。

答案 3 :(得分:1)

SWIFT 3

决定是否应启用(或隐藏)相机按钮您应该检查:

    if UIImagePickerController.isSourceTypeAvailable(.Camera){ }

但接下来我会查看用户是否允许访问相机,就像Apple在PhotoPicker示例中所建议的那样(PhotoPicker example 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)
}

答案 4 :(得分:-6)

如其他地方所述,尽管枚举中存在“限制”值,但检查AVAuthorizationStatus实际上并不会告诉您它是否受到限制。 相反,我发现检查源是否有用是有效的:

BOOL isCameraAvailable = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];

如果isCameraAvailableNO,则用户很可能在限制中禁用了相机。见Detect existence of camera in iPhone app?

相关问题