【问题标题】:Not able to show the alert if user denied the camera access如果用户拒绝摄像头访问,则无法显示警报
【发布时间】:2019-02-18 11:10:56
【问题描述】:

如果用户拒绝访问相机,我将显示一个带有取消和设置按钮的警报以显示它。但是代码不起作用。

@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 方法来显示我的弹出窗口?。

有什么想法吗?

【问题讨论】:

标签: ios objective-c iphone xcode alert


【解决方案1】:

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

第一步

在您的项目中添加 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()
}
}

基于上述动作条件将满足,

如果用户从未响应访问他/她的相机的请求,您需要通过 iOS 系统警报提示请求权限:

 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)
}

要使用相机捕获的图像,您需要设置视图控制器以遵守并实现几个委托协议:

 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)
}

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2016-12-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多