dav*_*vid 1 iphone xcode alert objective-c ios
如果用户拒绝相机访问,我将显示一个带有取消和设置按钮的警报以显示它。但代码不起作用。
@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)
}
Run Code Online (Sandbox Code Playgroud)
在我上面的代码中,我的方法是checkCameraPermission调用它来显示警报。我需要在用户单击相机时显示,如果用户拒绝则显示黑屏而不是相机。我需要显示弹出的警报。
我可以在哪里调用此checkCameraPermission方法来显示我的弹出窗口?
任何想法 ?
出于参考目的,我从本教程中获取了答案。
\n\n步骤1
\n\n在您的项目中添加 avfoundation 框架
\n\nimport AVFoundation\nRun Code Online (Sandbox Code Playgroud)\n\n第2步
\n\n不要忘记在 Info.plist 中设置相机使用说明
\n\n当您请求使用 device\xe2\x80\x99s 相机的权限时,默认的 iOS 系统对话框中会出现一条短消息。Privacy - Camera Usage Description您可以通过将密钥添加到文件来自定义此消息Info.plist。
步骤3
\n\n在您的图像配置文件更改按钮操作上验证权限等。
\n\n@IBAction func ProfileImageButton(_ sender: UIButton) {\n let cameraAuthorizationStatus = AVCaptureDevice.authorizationStatus(for: .video)\n switch cameraAuthorizationStatus {\ncase .notDetermined: requestCameraPermission()\ncase .authorized: presentCamera()\ncase .restricted, .denied: alertCameraAccessNeeded()\n}\n}\nRun Code Online (Sandbox Code Playgroud)\n\n基于上述操作,条件将满足,
\n\n\n\n\n如果用户从未响应过访问其相机的请求,则需要通过 iOS 系统警报提示请求权限:
\n
func requestCameraPermission() {\nAVCaptureDevice.requestAccess(for: .video, completionHandler: {accessGranted in\n guard accessGranted == true else { return }\n self.presentCamera()\n})\n}\nRun Code Online (Sandbox Code Playgroud)\n\n之后相机访问将继续
\n\n func presentCamera() {\nlet photoPicker = UIImagePickerController()\nphotoPicker.sourceType = .camera\nphotoPicker.delegate = self as? UIImagePickerControllerDelegate & UINavigationControllerDelegate\n\nself.present(photoPicker, animated: true, completion: nil)\n}\nRun Code Online (Sandbox Code Playgroud)\n\n\n\n\n要使用相机捕获的图像,您需要设置视图控制器以遵守并实现几个委托协议:
\n
class ViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate {\n // ...\n }\n\n func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {\nlet photo = info[UIImagePickerControllerOriginalImage] as! UIImage\n// do something with the photo... set to UIImageView, save it, etc.\n\ndismiss(animated: true, completion: nil)\n}\nRun Code Online (Sandbox Code Playgroud)\n\n如果相机访问被拒绝或限制,您可以提醒用户并将他们引导至“设置”应用程序以进行适当的权限调整:
\n\nfunc alertCameraAccessNeeded() {\nlet settingsAppURL = URL(string: UIApplicationOpenSettingsURLString)!\n\nlet alert = UIAlertController(\n title: "Need Camera Access",\n message: "Camera access is required to make full use of this app.",\n preferredStyle: UIAlertControllerStyle.alert\n)\n\nalert.addAction(UIAlertAction(title: "Cancel", style: .default, handler: nil))\nalert.addAction(UIAlertAction(title: "Allow Camera", style: .cancel, handler: { (alert) -> Void in\n UIApplication.shared.open(settingsAppURL, options: [:], completionHandler: nil)\n}))\n\npresent(alert, animated: true, completion: nil)\n}\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
5170 次 |
| 最近记录: |