且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-12-01 21:15:04

供参考我从 第1步

在您的项目中添加avfoundation框架

add the avfoundation framework in your project

import AVFoundation

第2步

不要忘记在Info.plist中设置相机使用说明

dont forget to Set Camera Usage Description in Info.plist

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

When you request permission to use the device’s camera, a short message will appear in the default iOS system dialog. You customize this message by adding the Privacy - Camera Usage Description key to your Info.plist file.

第3步

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

on your image profile change button action verify the permission, etc.

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

根据上述条件,该条件将满足

based on the above action the condition will satisfy,

如果用户从未响应过访问其相机的请求,则需要使用iOS系统警报提示以请求权限:

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

如果摄像机访问被拒绝或限制,则可以提醒用户并将其定向到设置"应用以进行适当的权限调整:

If camera access has been denied or restricted, you can alert the user and direct them to the Settings app to make the appropriate permissions adjustment:

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