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

时间:2021-10-10 06:40:48

I'm doing an ios app with a button that launch the camera.

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

I want to enable/disable the button if the device has a camera available or not.

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

I want to detect if the device has a camera and also when the device has camera but it's restricted (with this) so you can't use it.

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

How can I detect these two options?

我该如何检测这两个选项?

Thanks

5 个解决方案

#1


-6  

As stated elsewhere, checking the AVAuthorizationStatus won't actually tell you if it's restricted, despite the presence of a "restricted" value in the enum. Instead, I have found that checking if source is enabled to be helpful:

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

BOOL isCameraAvailable = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];

If isCameraAvailable is NO then the user has most likely disabled camera in Restrictions. See Detect existence of camera in iPhone app?

如果isCameraAvailable为NO,那么用户很可能在限制中禁用了相机。请参阅在iPhone应用程序中检测相机的存在?

#2


26  

To check camera permission status in app use following snippet.

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

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

#3


4  

To check whether camera restricted AVAuthorizationStatus is not enough. As said in documentation:

检查相机是否限制AVAuthorizationStatus是不够的。如文档中所述:

This status is normally not visible—the AVCaptureDevice class methods for discovering devices do not return devices the user is restricted from accessing.

此状态通常不可见 - 用于发现设备的AVCaptureDevice类方法不会返回用户被限制访问的设备。

So for proper check you need to create some capture device, for example, as I did:

因此,为了正确检查,您需要创建一些捕获设备,例如,正如我所做的那样:

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;
    }
}

#4


3  

The first time the user tries to use to camera on ios 6 he/she is automatically asked for permission. You don't have to add extra code (before that the authorisationstatus is ALAuthorizationStatusNotDetermined ).

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

So if user denies the first time you cannot ask again.

因此,如果用户第一次否认你不能再问。

You can use ALAssetsLibrary to check this. Check this answer for this solutions : ask-permission-to-access-camera

您可以使用ALAssetsLibrary来检查这一点。检查此解决方案的答案:ask-permission-to-access-camera

Hope it helps you.

希望它能帮到你。

#5


1  

SWIFT 3

To decide if the camera button should even be enabled (or hidden)You should check the:

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

    if UIImagePickerController.isSourceTypeAvailable(.Camera){ }

But then I would check to see if the user allowed access to camera as apple suggests in their PhotoPicker example (PhotoPicker example Objective-C):

但后来我会检查用户是否允许访问相机,就像Apple在PhotoPicker示例中所建议的那样(PhotoPicker示例Objective-C):

*please note you have to import AVFoundation

*请注意您必须导入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)
}

#1


-6  

As stated elsewhere, checking the AVAuthorizationStatus won't actually tell you if it's restricted, despite the presence of a "restricted" value in the enum. Instead, I have found that checking if source is enabled to be helpful:

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

BOOL isCameraAvailable = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];

If isCameraAvailable is NO then the user has most likely disabled camera in Restrictions. See Detect existence of camera in iPhone app?

如果isCameraAvailable为NO,那么用户很可能在限制中禁用了相机。请参阅在iPhone应用程序中检测相机的存在?

#2


26  

To check camera permission status in app use following snippet.

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

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

#3


4  

To check whether camera restricted AVAuthorizationStatus is not enough. As said in documentation:

检查相机是否限制AVAuthorizationStatus是不够的。如文档中所述:

This status is normally not visible—the AVCaptureDevice class methods for discovering devices do not return devices the user is restricted from accessing.

此状态通常不可见 - 用于发现设备的AVCaptureDevice类方法不会返回用户被限制访问的设备。

So for proper check you need to create some capture device, for example, as I did:

因此,为了正确检查,您需要创建一些捕获设备,例如,正如我所做的那样:

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;
    }
}

#4


3  

The first time the user tries to use to camera on ios 6 he/she is automatically asked for permission. You don't have to add extra code (before that the authorisationstatus is ALAuthorizationStatusNotDetermined ).

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

So if user denies the first time you cannot ask again.

因此,如果用户第一次否认你不能再问。

You can use ALAssetsLibrary to check this. Check this answer for this solutions : ask-permission-to-access-camera

您可以使用ALAssetsLibrary来检查这一点。检查此解决方案的答案:ask-permission-to-access-camera

Hope it helps you.

希望它能帮到你。

#5


1  

SWIFT 3

To decide if the camera button should even be enabled (or hidden)You should check the:

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

    if UIImagePickerController.isSourceTypeAvailable(.Camera){ }

But then I would check to see if the user allowed access to camera as apple suggests in their PhotoPicker example (PhotoPicker example Objective-C):

但后来我会检查用户是否允许访问相机,就像Apple在PhotoPicker示例中所建议的那样(PhotoPicker示例Objective-C):

*please note you have to import AVFoundation

*请注意您必须导入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)
}