相机&照片库权限 - 当用户不接受允许iOS 10时处理

时间:2017-06-19 04:52:27

标签: ios swift camera avfoundation ios10

我要求用户接受对照片库和相机使用的访问权限。我希望能够处理用户不接受的情况,但我遇到了问题。以下是检查用户权限时的代码:

AVCaptureDevice.requestAccess(forMediaType: AVMediaTypeVideo) { granted in
        if granted {
            if(!self.captureSession.isRunning){
                self.setupCustomCamera()
            }
        } else {
            self.takePhotoButton.alpha = 0.5
            self.takePhotoButton.isEnabled = false
            self.showNeedAccessMessage()
        }
}

我的showNeedAccessMessage()如下:

func showNeedAccessMessage() {
        let alert = UIAlertController(title: "Camera Settings", message: "Please adjust your device settings to grant access to camera use.", preferredStyle: .alert)

        alert.addAction(UIAlertAction(title: "OK", style: .cancel, handler: { (action: UIAlertAction) -> Void in
            self.dismiss(animated: true, completion: nil)
        }))

        show(alert, sender: nil)
}

这里的问题是,当granted案例未得到满足时,我想显示警告。我的应用程序尝试打开“图像设置”页面而不是我的警报显示,该页面显示如下: enter image description here

此案例是否有默认处理?如果是这样,任何想法如何修复黑色'图像设置'屏幕?

提前谢谢!

1 个答案:

答案 0 :(得分:8)

 AVCaptureDevice.requestAccess(forMediaType: AVMediaTypeVideo, completionHandler: { (granted :Bool) -> Void in
                if granted == true
                {
                    // User granted
                }
                else
                {
                    let alert = UIAlertController(title: "Error", message: "This app is not authorized to use Camera.", preferredStyle: .alert)

                    alert.addAction(UIAlertAction(title: "Setting", style: .default, handler: { (_) in
                        DispatchQueue.main.async {
                            if let settingsURL = URL(string: UIApplicationOpenSettingsURLString) {
                                UIApplication.shared.openURL(settingsURL)
                            }
                        }
                    }))
                    alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
                    self.present(alert, animated: true, completion: nil)

                    return
                }
            });