【发布时间】: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 方法来显示我的弹出窗口?。
有什么想法吗?
【问题讨论】:
-
你击中的地方
-
也可以查看这里以获得帮助:andrewcbancroft.com/2018/02/24/…
-
@Anbu.Karthik 是的,在那之后只有我写了。无法解决
-
你遇到了什么问题。你需要什么帮助
标签: ios objective-c iphone xcode alert