gpt4 book ai didi

ios - Swift:在相机访问请求后转换到其他 View Controller 时崩溃

转载 作者:行者123 更新时间:2023-11-30 12:05:55 25 4
gpt4 key购买 nike

问题是这样的:有两个 UIViewController:“WelcomeScreen”和“FailScreen”。第一个有一个相机访问请求。用户点击“不允许”并打开失败屏幕。 ...并且崩溃。

代码:

AVCaptureDevice.requestAccess(for: .video) { (answer: Bool) in
print("Camera access request.")
if answer {
print("Camera access autorized.")
// Continue to Notifications Request...
}
else {
print("Camera access denied.")
self.present(FailureViewController(), animated: true, completion: nil)
}

控制台:

Camera access denied.
libc++abi.dylib: terminating with uncaught exception of type NSException
  • 在通知请求(紧邻摄像头访问请求)之后,以同样的方式转换到其他 ViewController 会崩溃。
  • 我是菜鸟。 :)
  • 我尝试了present(VC)、show(VC)、push(VC)。结果是一样的。
  • 我没有使用 Storyboard 。我以编程方式完成所有事情。
  • 是的,我可以在同一个 ViewController 上显示 FailScreen,但是有其他选项可以避免此错误吗?

有人可以告诉我如何解决吗?谢谢。

最佳答案

希望这对您有帮助。

首先,您应该在 Info.plist 中为 Privacy - Camera Use Description 键添加值并了解以下代码

 AVCaptureDevice.requestAccess(forMediaType: AVMediaTypeVideo, completionHandler: { (success) in

if success {
print("The user granted permission")

} else {
print("put up an alert telling the user the camera is not available")

DispatchQueue.main.async(execute: { () -> Void in
let ac = UIAlertController(title: "Camera Error", message: "For some reason, the camera in this device is not accepting your authorization. Check with your device supplier.", preferredStyle: .alert)
ac.addAction(UIAlertAction(title: "Ok", style: .default, handler: nil))
self.present(ac, animated: true, completion: nil)
})//back on main queue block
}//if success

})//requestAccessForMediaType block


}//switch

} else {//if device has a camera

let ac = UIAlertController(title: "Source not available.", message: "The camera is not available.", preferredStyle: .alert)
ac.addAction(UIAlertAction(title: "Ok", style: .default, handler: nil))
present(ac, animated: true, completion: nil)

}//if camera is no else

}

关于ios - Swift:在相机访问请求后转换到其他 View Controller 时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46685914/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com