【问题标题】:Camera & Photo Library Permission - Handle when a user does not accept to allow iOS 10相机和照片库权限 - 当用户不接受允许 iOS 10 时处理
【发布时间】:2017-06-19 04:52:27
【问题描述】:

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

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 的情况不满足时,我想显示一个警报。我的应用程序没有显示警报,而是尝试打开“图像设置”页面,该页面显示如下:

这种情况有默认处理方式吗?如果是这样,有什么想法可以解决黑色的“图像设置”屏幕吗?

提前谢谢你!

【问题讨论】:

    标签: ios swift camera avfoundation ios10


    【解决方案1】:
     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
                    }
                });
    

    【讨论】:

      猜你喜欢
      • 2017-03-10
      • 2021-03-06
      • 2017-04-15
      • 2021-05-27
      • 2020-02-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-22
      相关资源
      最近更新 更多