@@ -40,33 +40,46 @@ class QRScannerController: UIViewController, AVCaptureMetadataOutputObjectsDeleg
4040
4141 captureSession. startRunning ( )
4242
43- NotificationCenter . default. addObserver ( self , selector: #selector( self . loadPreview) ,
44- name: NSNotification . Name. UIDeviceOrientationDidChange, object: nil )
43+ NotificationCenter . default. addObserver ( self , selector: #selector( loadPreview) ,
44+ name: Notification . Name. UIDeviceOrientationDidChange, object: nil )
4545 }
4646 }
4747
4848 override func viewWillAppear( _ animated: Bool ) {
49+ NotificationCenter . default. addObserver ( self , selector: #selector( loadTheme) ,
50+ name: Notification . Name. UIApplicationDidBecomeActive, object: nil )
4951 loadTheme ( )
5052 }
5153
54+ deinit {
55+ if #available( iOS 9 . 0 , * ) { }
56+ else {
57+ NotificationCenter . default. removeObserver ( self )
58+ }
59+ }
60+
5261 // MARK: AVCaptureMetadataOutputObjectsDelegate
5362
5463 func captureOutput( _ captureOutput: AVCaptureOutput ! , didOutputMetadataObjects metadataObjects: [ Any ] ! , from connection: AVCaptureConnection ! ) {
5564
5665 if !codeIsRead {
5766
58- let metadataObj = metadataObjects. first as! AVMetadataMachineReadableCodeObject
67+ let metadataObject = metadataObjects. first as? AVMetadataMachineReadableCodeObject
5968
60- if metadataObj. type == AVMetadataObjectTypeQRCode {
61-
62- // READ JSON format
63-
64- if let data = metadataObj. stringValue. data ( using: . utf8) {
65- let content = try ? JSONSerialization . jsonObject ( with: data, options: . allowFragments) as! [ [ String : Any ] ]
66- performSegue ( withIdentifier: " unwindToQuestions " , sender: content)
67- codeIsRead = true
68- }
69+ guard let metadata = metadataObject, metadata. type == AVMetadataObjectTypeQRCode else { invalidQRCodeFormat ( ) ; return }
70+ guard let data = metadata. stringValue. data ( using: . utf8) else { invalidQRCodeFormat ( ) ; return }
71+
72+ var content : [ [ String : Any ] ] ?
73+
74+ do {
75+ content = try JSONSerialization . jsonObject ( with: data, options: . allowFragments) as? [ [ String : Any ] ]
76+ } catch { invalidQRCodeFormat ( ) ; } //return }
77+
78+ if let validContent = content {
79+ performSegue ( withIdentifier: " unwindToQuestions " , sender: validContent)
80+ codeIsRead = true
6981 }
82+ else { invalidQRCodeFormat ( ) ; }
7083 }
7184 }
7285
@@ -83,6 +96,8 @@ class QRScannerController: UIViewController, AVCaptureMetadataOutputObjectsDeleg
8396
8497 @IBAction func unwindToQRScanner( _ segue: UIStoryboardSegue ) { }
8598
99+ // MARK: Alerts
100+
86101 @IBAction func allowCameraAction( ) {
87102 let alertViewController = UIAlertController ( title: " Attention " . localized,
88103 message: " Camera access required for QR Scanning " . localized,
@@ -123,4 +138,13 @@ class QRScannerController: UIViewController, AVCaptureMetadataOutputObjectsDeleg
123138 view. backgroundColor = darkThemeEnabled ? . gray : . white
124139 allowCameraButton. setTitleColor ( darkThemeEnabled ? . warmYellow : . coolBlue, for: . normal)
125140 }
141+
142+ func invalidQRCodeFormat( ) {
143+ let alertViewController = UIAlertController ( title: " Attention " . localized,
144+ message: " Invalid QR Code format " ,
145+ preferredStyle: . alert)
146+
147+ alertViewController. addAction ( title: " OK " . localized, style: . default, handler: nil )
148+ present ( alertViewController, animated: true , completion: nil )
149+ }
126150}
0 commit comments