【问题标题】:How can I detect if camera is restricted by user如何检测相机是否受用户限制
【发布时间】:2013-05-27 04:54:32
【问题描述】:

我正在做一个带有启动相机按钮的 ios 应用程序。

如果设备有可用的摄像头,我想启用/禁用按钮。

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

如何检测这两个选项?

谢谢

【问题讨论】:

  • 你为什么不自己测试一下呢?为什么要发布问题?
  • 我已经测试过了,我能够检测到设备是否有摄像头,但我无法检测到设备何时有摄像头,但它受到限制,因此您无法使用它。
  • 对不起,我想我误读了你的所作所为。我以为您在问如果相机受到限制,该代码是否有效。这就是为什么我建议你简单地测试它。出于好奇,如果您设置了一个带有受限摄像头的设备,isSourceTypeAvailable 会返回什么?
  • 对不起,我是 ios 的新手,我不知道我以前是如何尝试的。现在我又试了一次,当相机不受限制时 isSourceTypeAvailable 返回 YES ,当它受到限制时返回 NO ,正如预期的那样。所以,这个问题没有意义。对不起。
  • 我添加了一个使用isSourceTypeAvailable 的答案,以便其他人可以轻松查看该做什么。如果您认为足够,请接受该答案!

标签: ios camera restrictions


【解决方案1】:

要检查应用程序中的相机权限状态,请使用以下 sn-p。

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

【讨论】:

  • 确保导入AVFoundation @import AVFoundation;
  • 这对于有问题的场景不起作用(至少在我测试过的 iOS 8.4 上)。如果您为该应用启用了摄像头但整体受到限制,它将返回AVAuthorizationStatusAuthorized,这是不正确的。老实说,我不确定为什么 AVAuthorizationStatusRestricted 存在!
  • @nickjm 可能就是这样。你提到的情况我还没有测试过。
  • 这实际上是正确答案的一半,它在 iOS 9 中确实有效。有 3 种状态可以禁用相机。 1.没有摄像头,2.专门为此应用禁用了摄像头,3.通过限制禁用了摄像头。仅此捕获案例 2,但不捕获案例 1 和 3。[UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera] 捕获案例 1 和 3,但不捕获案例 2。如果您想检查所有可能的原因,相机可能不可用,您需要实现此 AND @ 987654326@
【解决方案2】:

SWIFT 3

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

    if UIImagePickerController.isSourceTypeAvailable(.Camera){ }

然后我会检查用户是否允许访问相机,正如苹果在他们的 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)
}

【讨论】:

    【解决方案3】:

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

    BOOL isCameraAvailable = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];
    

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

    【讨论】:

      【解决方案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;
          }
      }
      

      【讨论】:

        【解决方案5】:

        用户第一次尝试在 ios 6 上使用相机时,系统会自动要求他/她获得许可。您不必添加额外的代码(在授权状态为 ALAuthorizationStatusNotDetermined 之前)。

        所以如果用户第一次拒绝你就不能再问了。

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

        希望对你有帮助。

        【讨论】:

        • 似乎 ALAssetsLibrary 只是告诉您是否可以访问照片,而不是相机。
        猜你喜欢
        • 2021-05-16
        • 2015-10-27
        • 1970-01-01
        • 2011-03-10
        • 1970-01-01
        • 1970-01-01
        • 2016-12-10
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多