I\'m having some issues trying to use the camera. The problem is that some devices show me the Camera entry under settings, and some others don\'t. In those devices where the Ca
I need the camera to user ARKit
. Every time the vc with sceneView is shown I run the checkCameraPermission()
in viewWillAppear
. If the user doesn't allow access an alert will show with a settingsButton. Once they press that button they will be taken to Settings
and the camera icon with the toggle switch will always be there.
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
checkCameraPermission()
}
func checkCameraPermission() {
let authorizationStatus = AVCaptureDevice.authorizationStatus(for: AVMediaType.video)
switch authorizationStatus {
case .notDetermined:
AVCaptureDevice.requestAccess(for: AVMediaType.video) { [weak self](granted) in
if granted {
print("access granted")
DispatchQueue.main.async { [weak self] in
self?.startSceneViewSession()
}
} else {
print("access denied")
DispatchQueue.main.async { [weak self] in
self?.alertUserCameraPermissionMustBeEnabled()
}
}
}
case .authorized:
print("Access authorized")
startSceneViewSession()
case .denied, .restricted:
print("restricted")
alertUserCameraPermissionMustBeEnabled()
@unknown default:
fatalError()
}
}
func alertUserCameraPermissionMustBeEnabled() {
let message = "Camera access is necessary to use Augemented Reality for this app.\n\nPlease go to Settings to allow access to the Camera.\n Please switch the button to the green color."
let alert = UIAlertController (title: "Camera Access Required", message: message, preferredStyle: .alert)
let settingsAction = UIAlertAction(title: "Settings", style: .default, handler: { (action) in
guard let settingsUrl = URL(string: UIApplication.openSettingsURLString) else { return }
if UIApplication.shared.canOpenURL(settingsUrl) {
UIApplication.shared.open(settingsUrl, completionHandler: { (_) in
})
}
})
alert.addAction(settingsAction)
present(alert, animated: true, completion: nil)
}
// this is for ARKit
func startSceneViewSession() {
sceneView.session.run(configuration)
sceneView.isPlaying = true
}