首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >弹出窗口不会请求iOS 12中的相机访问权限

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

Stack Overflow用户
提问于 2018-10-03 07:58:31
回答 1查看 6K关注 0票数 6

按照苹果的标准,我们需要获得用户相机的许可。因此,我已经成功地整合了相机,而且它在iOS 11中运行良好。但目前,我正在测试相机功能,发现如果用户一次允许相机访问,即使在重新安装(从应用程序商店)之后,相同的应用程序也不会请求许可。

所以我的问题是,在iOS 12中它的行为是否发生了变化,还是每次用户试图安装新的应用程序时,我们都需要做一些设置以获得许可?

谢谢

EN

回答 1

Stack Overflow用户

发布于 2018-11-30 20:08:35

iOS 12.1 / Swift 4.2

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

OnCameraOpenButtonTap()

代码语言:javascript
运行
复制
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)
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52622390

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档