@@ -34,24 +34,46 @@ class ViewController: UIViewController, QRCodeReaderViewControllerDelegate {
34
34
} )
35
35
36
36
@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
40
41
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
+ }
44
46
}
45
- }
46
47
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 ) )
52
71
53
- present ( alert, animated: true , completion: nil )
72
+ present ( alert, animated: true , completion: nil )
73
+ default : ( )
74
+ }
54
75
}
76
+
55
77
}
56
78
57
79
// MARK: - QRCodeReader Delegate Methods
0 commit comments