Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/112.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在使用摄像机之前,向用户请求使用摄像机的权限_Ios_Ipad - Fatal编程技术网

Ios 在使用摄像机之前,向用户请求使用摄像机的权限

Ios 在使用摄像机之前,向用户请求使用摄像机的权限,ios,ipad,Ios,Ipad,在IOS 8中,用户现在被要求获得使用设备摄像头的权限。这很好,但我的问题是,我想在用户实际需要摄像头之前(当用户第一次加载应用程序时)就请求权限。我知道这可以在事先请求用户访问摄像头卷的权限时完成。应用程序只在实际访问摄像头时请求权限,因此您必须访问它。当然,您可以在AVCaptureSession的后台执行此操作,而不显示您获得的图像。 下面简单介绍如何使用AVCaptureSession实现此目的。 我不会使用这个,因为用户不知道相机是用来做什么的——除了在明显的例子中(相机应用程序)。用

在IOS 8中,用户现在被要求获得使用设备摄像头的权限。这很好,但我的问题是,我想在用户实际需要摄像头之前(当用户第一次加载应用程序时)就请求权限。我知道这可以在事先请求用户访问摄像头卷的权限时完成。

应用程序只在实际访问摄像头时请求权限,因此您必须访问它。当然,您可以在AVCaptureSession的后台执行此操作,而不显示您获得的图像。 下面简单介绍如何使用AVCaptureSession实现此目的。
我不会使用这个,因为用户不知道相机是用来做什么的——除了在明显的例子中(相机应用程序)。用户可能会感到困惑,为什么相机会打开。您可以创建一个视图,描述您使用相机的目的,然后在后台激活相机以提示用户,也可以保持原样,因为这不需要花费太多时间

为了回答我自己的问题以帮助其他人解决类似问题,我使用了以下代码-非常有效,因为在创建imagePicker之前,is会向用户请求使用设备摄像头的权限

   AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
AVCaptureDeviceInput *deviceInput = [AVCaptureDeviceInput deviceInputWithDevice:device error:NULL];
if (deviceInput)
{
    // user has excepted camera


} else {

    // user has NOT excepted camera


}

以下是最新答案(Swift 5):


这方面没有API。如果你有一个好的用例,我建议你向苹果公司提出一个增强请求。我不知道你在这方面的具体用例,但苹果公司的设计指南指出,断章取义的弹出窗口可能会令人不安,他们建议不要这样做。我特别读到了关于在发布时提示苹果ID恢复应用程序内购买的内容,虽然这并没有那么糟糕,但值得考虑这个建议。@matt
AVCaptureDevice
有一个类方法
requestAccessForMediaType
,可以做到这一点。@Aalbert-我尝试了示例代码,但它不起作用-尽管如此,您的回答让我想到在初始viewController中创建一个imagePicker,然后设置[imagePicker.view setHidden:YES];这会在用户不使用相机的情况下提示用户相机警报。它可以工作,但有点凌乱!!这是不对的。使用AVCatpureDevice requestAccessForMediaType:completionHandler:。
private func openCamera() {
    switch AVCaptureDevice.authorizationStatus(for: .video) {
    case .authorized: // the user has already authorized to access the camera.
        self.setupCaptureSession()

    case .notDetermined: // the user has not yet asked for camera access.
        AVCaptureDevice.requestAccess(for: .video) { (granted) in
            if granted { // if user has granted to access the camera.
                print("the user has granted to access the camera")
                DispatchQueue.main.async {
                    self.setupCaptureSession()
                }
            } else {
                print("the user has not granted to access the camera")
                self.handleDismiss()
            }
        }

    case .denied:
        print("the user has denied previously to access the camera.")
        self.handleDismiss()

    case .restricted:
        print("the user can't give camera access due to some restriction.")
        self.handleDismiss()

    default:
        print("something has wrong due to we can't access the camera.")
        self.handleDismiss()
    }
}