Skip to content

Commit 255280a

Browse files
authored
Merge pull request yannickl#85 from lgw51/master
May be case not authorized to use Back Camera.
2 parents 96c0d7a + 45d825f commit 255280a

File tree

2 files changed

+37
-15
lines changed

2 files changed

+37
-15
lines changed

Example/QRCodeReader.swift/ViewController.swift

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,24 +34,46 @@ class ViewController: UIViewController, QRCodeReaderViewControllerDelegate {
3434
})
3535

3636
@IBAction func scanAction(_ sender: AnyObject) {
37-
if QRCodeReader.supportsMetadataObjectTypes() {
38-
reader.modalPresentationStyle = .formSheet
39-
reader.delegate = self
37+
do {
38+
if try QRCodeReader.supportsMetadataObjectTypes() {
39+
reader.modalPresentationStyle = .formSheet
40+
reader.delegate = self
4041

41-
reader.completionBlock = { (result: QRCodeReaderResult?) in
42-
if let result = result {
43-
print("Completion with result: \(result.value) of type \(result.metadataType)")
42+
reader.completionBlock = { (result: QRCodeReaderResult?) in
43+
if let result = result {
44+
print("Completion with result: \(result.value) of type \(result.metadataType)")
45+
}
4446
}
45-
}
4647

47-
present(reader, animated: true, completion: nil)
48-
}
49-
else {
50-
let alert = UIAlertController(title: "Error", message: "Reader not supported by the current device", preferredStyle: .alert)
51-
alert.addAction(UIAlertAction(title: "OK", style: .cancel, handler: nil))
48+
present(reader, animated: true, completion: nil)
49+
}
50+
} catch let error as NSError {
51+
switch error.code {
52+
case -11852:
53+
54+
let alert = UIAlertController(title: "Error", message: "This app is not authorized to use Back Camera.", preferredStyle: .alert)
55+
56+
alert.addAction(UIAlertAction(title: "Setting", style: .default, handler: { (_) in
57+
DispatchQueue.main.async {
58+
if let settingsURL = URL(string: UIApplicationOpenSettingsURLString) {
59+
UIApplication.shared.openURL(settingsURL)
60+
}
61+
}
62+
}))
63+
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
64+
present(alert, animated: true, completion: nil)
65+
66+
67+
68+
case -11814:
69+
let alert = UIAlertController(title: "Error", message: "Reader not supported by the current device", preferredStyle: .alert)
70+
alert.addAction(UIAlertAction(title: "OK", style: .cancel, handler: nil))
5271

53-
present(alert, animated: true, completion: nil)
72+
present(alert, animated: true, completion: nil)
73+
default:()
74+
}
5475
}
76+
5577
}
5678

5779
// MARK: - QRCodeReader Delegate Methods

Sources/QRCodeReader.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -306,10 +306,10 @@ public final class QRCodeReader: NSObject, AVCaptureMetadataOutputObjectsDelegat
306306

307307
- returns: A boolean value that indicates whether the device supports the given metadata object types.
308308
*/
309-
public class func supportsMetadataObjectTypes(_ metadataTypes: [String]? = nil) -> Bool {
309+
public class func supportsMetadataObjectTypes(_ metadataTypes: [String]? = nil) throws -> Bool {
310310
let captureDevice = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)
311311

312-
guard let deviceInput = try? AVCaptureDeviceInput(device: captureDevice) else { return false }
312+
let deviceInput = try AVCaptureDeviceInput(device: captureDevice)
313313

314314
let output = AVCaptureMetadataOutput()
315315
let session = AVCaptureSession()

0 commit comments

Comments
 (0)