如果用户拒绝相机访问,则无法显示警报

Not able to show the alert if user denied the camera access

如果用户拒绝摄像头访问,我会显示一个带有取消和设置按钮的警报来显示它。但是代码不起作用。

@IBAction func ProfileImageButton(_ sender: UIButton) {
        print("profile image Button is pressed")
        let imagePickerController = UIImagePickerController()
        imagePickerController.delegate = self
        profileimgbool = true
        let actionSheet = UIAlertController(title: "Photo Source", message: "choose a Source", preferredStyle: .actionSheet)

        actionSheet.addAction(UIAlertAction(title: "Camera", style: .default, handler: {(action:UIAlertAction) in imagePickerController.sourceType = .camera
            self.present(imagePickerController, animated: true, completion: nil)

        }))

        actionSheet.addAction(UIAlertAction(title: "Photo Library", style: .default, handler: {(action:UIAlertAction) in imagePickerController.sourceType = .photoLibrary
            self.present(imagePickerController, animated: true, completion: nil)}))

        actionSheet.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))



        self.present(actionSheet, animated: true, completion: nil)

    }

 func checkCameraPermission()  {
        let cameraMediaType = AVMediaType.video
        AVCaptureDevice.requestAccess(for: cameraMediaType) { granted in
            if granted {
                //Do operation
                print("Granted access for camera")
               // self.setCamera()
            } else {
                self.noCameraFound()
                print("Denied access for camera ")
            }
        }
    }
    func noCameraFound(){
        let alert = UIAlertController(title: "CallDoc", message: "Please allow camera access in phone settings", preferredStyle: UIAlertControllerStyle.alert)
        alert.addAction(UIAlertAction(title: "Back", style: UIAlertActionStyle.cancel, handler: {(action:UIAlertAction) in


        }));

        alert.addAction(UIAlertAction(title: "Open setting", style: UIAlertActionStyle.default, handler: {(action:UIAlertAction) in
            UIApplication.shared.open(NSURL(string:UIApplicationOpenSettingsURLString)! as URL, options: [:], completionHandler: nil)

        }));
        self.present(alert, animated: true, completion: nil)
    }

在我上面的代码中,我的方法是 checkCameraPermission 我将调用它来显示警报。我需要在用户单击相机时显示,以及当用户拒绝时显示黑屏而不是相机。我需要显示弹出的警报。

我在哪里可以调用此 checkCameraPermission 方法来显示我的弹出窗口?

有什么想法吗?

出于参考目的,我从 this 教程中获取了答案。

步骤 1

在您的项目中添加 avfoundation 框架

import AVFoundation

步骤 2

不要忘记在 Info.plist

中设置相机使用说明

当您请求使用设备相机的权限时,默认的 iOS 系统对话框中会出现一条短消息。您可以通过将 Privacy - Camera Usage Description 键添加到 Info.plist 文件来自定义此消息。

步骤 3

在您的图像配置文件更改按钮操作上验证权限等

@IBAction func ProfileImageButton(_ sender: UIButton) {
 let cameraAuthorizationStatus = AVCaptureDevice.authorizationStatus(for: .video)
  switch cameraAuthorizationStatus {
case .notDetermined: requestCameraPermission()
case .authorized: presentCamera()
case .restricted, .denied: alertCameraAccessNeeded()
}
}

根据上面的动作,条件就满足了,

If the user has never responded to a request to access his/her camera, you need to prompt with the iOS system alert to request permission:

 func requestCameraPermission() {
AVCaptureDevice.requestAccess(for: .video, completionHandler: {accessGranted in
    guard accessGranted == true else { return }
    self.presentCamera()
})
}

之后相机访问将继续

 func presentCamera() {
let photoPicker = UIImagePickerController()
photoPicker.sourceType = .camera
photoPicker.delegate = self as? UIImagePickerControllerDelegate & UINavigationControllerDelegate

self.present(photoPicker, animated: true, completion: nil)
}

To use the image that the camera captured, you need to set up your view controller to adhere to and implement couple of delegate protocols:

 class ViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
  // ...
 }

 func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let photo = info[UIImagePickerControllerOriginalImage] as! UIImage
// do something with the photo... set to UIImageView, save it, etc.

dismiss(animated: true, completion: nil)
}

如果相机访问被拒绝或限制,您可以提醒用户并将他们引导至“设置”应用以进行适当的权限调整:

func alertCameraAccessNeeded() {
let settingsAppURL = URL(string: UIApplicationOpenSettingsURLString)!

let alert = UIAlertController(
    title: "Need Camera Access",
    message: "Camera access is required to make full use of this app.",
    preferredStyle: UIAlertControllerStyle.alert
)

alert.addAction(UIAlertAction(title: "Cancel", style: .default, handler: nil))
alert.addAction(UIAlertAction(title: "Allow Camera", style: .cancel, handler: { (alert) -> Void in
    UIApplication.shared.open(settingsAppURL, options: [:], completionHandler: nil)
}))

present(alert, animated: true, completion: nil)
}