在使用相机之前要求用户使用相机

时间:2014-09-23 23:22:15

标签: ios ipad

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

3 个答案:

答案 0 :(得分:3)

应用程序仅询问权限是指实际访问摄像机的时间,因此您必须访问它。您当然可以在后台使用AVCaptureSession执行此操作,但不显示您获得的图像。 这是一个关于如何使用AVCaptureSession来实现此目的的简单example。 我不会使用它,因为用户不知道相机的用途 - 除了明显的例子(相机应用程序)。用户可能会感到困惑,为什么打开相机。您可以制作一个视图,描述您使用相机的内容,然后在后台激活相机以提示用户,或保持原样,因为它不会花费太多时间。

答案 1 :(得分:1)

要回答我自己的问题以帮助其他类似问题的人,我使用以下代码 - 效果很好,因为要求用户在创建imagePicker之前获得使用设备相机的权限。

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


} else {

    // user has NOT excepted camera


}

答案 2 :(得分:0)

以下是更新后的答案(快速5):

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()
    }
}
相关问题