Output error messages when camera is disabled.

This commit is contained in:
Yishi Lin 2018-08-30 00:42:38 +08:00
parent 29b8313be5
commit cacbeb57ee

View file

@ -31,9 +31,12 @@ class QRScannerController: UIViewController, AVCaptureMetadataOutputObjectsDeleg
override func viewDidLoad() { override func viewDidLoad() {
super.viewDidLoad() super.viewDidLoad()
if AVCaptureDevice.authorizationStatus(for: .video) == .denied {
presentCameraSettings()
}
// Get an instance of the AVCaptureDevice class to initialize a device object and provide the video as the media type parameter. // Get an instance of the AVCaptureDevice class to initialize a device object and provide the video as the media type parameter.
let captureDevice = AVCaptureDevice.default(for: AVMediaType.video) let captureDevice = AVCaptureDevice.default(for: AVMediaType.video)
do { do {
// Get an instance of the AVCaptureDeviceInput class using the previous device object. // Get an instance of the AVCaptureDeviceInput class using the previous device object.
let input = try AVCaptureDeviceInput(device: captureDevice!) let input = try AVCaptureDeviceInput(device: captureDevice!)
@ -78,6 +81,7 @@ class QRScannerController: UIViewController, AVCaptureMetadataOutputObjectsDeleg
} catch { } catch {
print(error) print(error)
scannerOutput.text = error.localizedDescription
return return
} }
} }
@ -125,4 +129,20 @@ class QRScannerController: UIViewController, AVCaptureMetadataOutputObjectsDeleg
scannerOutput.text = "No QR code detected" scannerOutput.text = "No QR code detected"
} }
} }
func presentCameraSettings() {
let alertController = UIAlertController(title: "Error",
message: "Camera access denied.\nWARNING: Toggle the camera permission resets the app! Save your changes.",
preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "Cancel", style: .default))
alertController.addAction(UIAlertAction(title: "Settings", style: .cancel) { _ in
if let url = URL(string: UIApplicationOpenSettingsURLString) {
UIApplication.shared.open(url, options: [:], completionHandler: { _ in
// Handle
})
}
})
present(alertController, animated: true)
}
} }