弹出窗口不会在 iOS 12 中请求访问相机的权限

Jat*_* JP 6 ios ios12

根据苹果标准,我们需要请求访问用户相机的权限。所以我已经成功地集成了摄像头并且它在 iOS 11 中运行良好。但目前,我正在测试摄像头功能,发现如果用户一次允许摄像头访问,即使在全新安装后,同一个应用程序也不会请求许可(来自应用商店)。

所以我的问题是,它在 iOS 12 中的行为是否发生了变化,还是每次用户尝试安装新应用时我们都需要进行一些设置以询问权限?

谢谢

Bor*_*lic 1

iOS 12.1 / 斯威夫特 4.2

每次用户点击应用程序中的“相机”按钮时,您都会调用此代码。它首先请求权限,如果过去安装的设置仍然存在,则会弹出 UIAlertController,允许用户打开设备上的“设置”应用程序,并更改相机权限设置。

OnCameraOpenButtonTap()

if UIImagePickerController.isSourceTypeAvailable(.camera) {
   checkCameraAccess(isAllowed: {
            if $0 {
                DispatchQueue.main.async {
                    self.presentCamera()
                }
            } else {
                DispatchQueue.main.async {
                self.presentCameraSettings()
            }
        }
    })
}

func checkCameraAccess(isAllowed: @escaping (Bool) -> Void) {
    switch AVCaptureDevice.authorizationStatus(for: .video) {
    case .denied:
        isAllowed(false)
    case .restricted:
        isAllowed(false)
    case .authorized:
        isAllowed(true)
    case .notDetermined:
        AVCaptureDevice.requestAccess(for: .video) { isAllowed($0) }
    }
}

private func presentCamera() {
    let imagePicker = UIImagePickerController()
    imagePicker.delegate = self
    imagePicker.sourceType = .camera
    present(imagePicker, animated: true, completion: nil)
}

private func presentCameraSettings() {
    let alert = UIAlertController.init(title: "allowCameraTitle", message: "allowCameraMessage", preferredStyle: .alert)
    alert.addAction(UIAlertAction.init(title: "Cancel", style: .cancel, handler: { (_) in
    }))

    alert.addAction(UIAlertAction.init(title: "Settings", style: .default, handler: { (_) in
        UIApplication.shared.open(URL(string: UIApplication.openSettingsURLString)!, options: [:], completionHandler: nil)
    }))

    present(alert, animated: true)
}
Run Code Online (Sandbox Code Playgroud)