2017-10-11 34 views
0

以下是问题: 有两个UIViewControllers:“WelcomeScreen”和例如“FailScreen”。 第一个有相机访问请求。 用户轻击“不允许”,FailScreen打开。 ...和崩溃。Swift:在摄像机访问请求后切换到其他视图控制器

代码:

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在通知请求后,以同样的方式崩溃(旁边的摄像机访问请求)。
  • 我是noob。 :)
  • 我试过礼物(VC),秀(VC),推(VC)。结果是一样的。
  • 我没有使用故事板。我以编程方式处理所有事情
  • 是的,我可以在同一个ViewController上显示FailScreen,但其他选项可以避免这种错误?

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

回答

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 

} 
+0

“摄像机使用说明”键被前加入。问题不在那里。 –

+0

我懂了!非常感谢你! –

+0

我的荣幸先生。 – Zee

相关问题