Swift:在Camera Access Request之后转换到其他视图控制器时崩溃

时间:2017-10-11 10:29:20

标签: ios swift uiviewcontroller notifications camera

问题在于: 有两个UIViewControllers:" WelcomeScreen"例如," FailScreen"。 第一个上有一个Camera Access Request。 用户点击"不允许"和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崩溃。
  • 我是菜鸟。 :)
  • 我试过现在(VC),秀(VC),推(VC)。结果是一样的。
  • 我没有使用Storyboard。我以编程方式完成所有工作。
  • 是的,我可以在同一个ViewController上显示FailScreen,但是其他选项可以避免这个错误呢?

有人可以告诉我,如何修复它?谢谢。

1 个答案:

答案 0 :(得分:0)

我希望这会对你有所帮助。

首先,您应该在Info.plist中为Privacy - Camera Usage 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

}