@@ -3,10 +3,10 @@ import AVFoundation
3
3
4
4
@objc ( QRScanner)
5
5
class QRScanner : CDVPlugin , AVCaptureMetadataOutputObjectsDelegate {
6
-
6
+
7
7
class CameraView : UIView {
8
8
var videoPreviewLayer : AVCaptureVideoPreviewLayer ?
9
-
9
+
10
10
func interfaceOrientationToVideoOrientation( _ orientation : UIInterfaceOrientation ) -> AVCaptureVideoOrientation {
11
11
switch ( orientation) {
12
12
case UIInterfaceOrientation . portrait:
@@ -29,18 +29,18 @@ class QRScanner : CDVPlugin, AVCaptureMetadataOutputObjectsDelegate {
29
29
layer. frame = self . bounds;
30
30
}
31
31
}
32
-
32
+
33
33
self . videoPreviewLayer? . connection? . videoOrientation = interfaceOrientationToVideoOrientation ( UIApplication . shared. statusBarOrientation) ;
34
34
}
35
-
36
-
35
+
36
+
37
37
func addPreviewLayer( _ previewLayer: AVCaptureVideoPreviewLayer ? ) {
38
38
previewLayer!. videoGravity = AVLayerVideoGravity . resizeAspectFill
39
39
previewLayer!. frame = self . bounds
40
40
self . layer. addSublayer ( previewLayer!)
41
41
self . videoPreviewLayer = previewLayer;
42
42
}
43
-
43
+
44
44
func removePreviewLayer( ) {
45
45
if self . videoPreviewLayer != nil {
46
46
self . videoPreviewLayer!. removeFromSuperlayer ( )
@@ -98,24 +98,14 @@ class QRScanner : CDVPlugin, AVCaptureMetadataOutputObjectsDelegate {
98
98
99
99
// utility method
100
100
@objc func backgroundThread( delay: Double = 0.0 , background: ( ( ) -> Void ) ? = nil , completion: ( ( ) -> Void ) ? = nil ) {
101
- if #available( iOS 8 . 0 , * ) {
102
- DispatchQueue . global ( qos: DispatchQoS . QoSClass. userInitiated) . async {
103
- if ( background != nil ) {
104
- background!( )
105
- }
106
- DispatchQueue . main. asyncAfter ( deadline: DispatchTime . now ( ) + delay * Double( NSEC_PER_SEC) ) {
107
- if ( completion != nil ) {
108
- completion!( )
109
- }
110
- }
111
- }
112
- } else {
113
- // Fallback for iOS < 8.0
114
- if ( background != nil ) {
101
+ DispatchQueue . global ( qos: DispatchQoS . QoSClass. userInitiated) . async {
102
+ if ( background != nil ) {
115
103
background!( )
116
104
}
117
- if ( completion != nil ) {
118
- completion!( )
105
+ DispatchQueue . main. asyncAfter ( deadline: DispatchTime . now ( ) + delay * Double( NSEC_PER_SEC) ) {
106
+ if ( completion != nil ) {
107
+ completion!( )
108
+ }
119
109
}
120
110
}
121
111
}
@@ -133,15 +123,8 @@ class QRScanner : CDVPlugin, AVCaptureMetadataOutputObjectsDelegate {
133
123
if ( captureSession? . isRunning != true ) {
134
124
cameraView. backgroundColor = UIColor . clear
135
125
self . webView!. superview!. insertSubview ( cameraView, belowSubview: self . webView!)
136
- let availableVideoDevices = AVCaptureDevice . devices ( for: AVMediaType . video)
137
- for device in availableVideoDevices {
138
- if device. position == AVCaptureDevice . Position. back {
139
- backCamera = device
140
- }
141
- else if device. position == AVCaptureDevice . Position. front {
142
- frontCamera = device
143
- }
144
- }
126
+ backCamera = AVCaptureDevice . default ( . builtInWideAngleCamera, for: . video, position: . back)
127
+ frontCamera = AVCaptureDevice . default ( . builtInWideAngleCamera, for: . video, position: . front)
145
128
// older iPods have no back camera
146
129
if ( backCamera == nil ) {
147
130
currentCamera = 1
@@ -467,8 +450,7 @@ class QRScanner : CDVPlugin, AVCaptureMetadataOutputObjectsDelegate {
467
450
}
468
451
469
452
@objc func openSettings( _ command: CDVInvokedUrlCommand ) {
470
- if #available( iOS 10 . 0 , * ) {
471
- guard let settingsUrl = URL ( string: UIApplicationOpenSettingsURLString) else {
453
+ guard let settingsUrl = URL ( string: UIApplication . openSettingsURLString) else {
472
454
return
473
455
}
474
456
if UIApplication . shared. canOpenURL ( settingsUrl) {
@@ -477,15 +459,6 @@ class QRScanner : CDVPlugin, AVCaptureMetadataOutputObjectsDelegate {
477
459
} )
478
460
} else {
479
461
self . sendErrorCode ( command: command, error: QRScannerError . open_settings_unavailable)
480
- }
481
- } else {
482
- // pre iOS 10.0
483
- if #available( iOS 8 . 0 , * ) {
484
- UIApplication . shared. openURL ( NSURL ( string: UIApplicationOpenSettingsURLString) ! as URL )
485
- self . getStatus ( command)
486
- } else {
487
- self . sendErrorCode ( command: command, error: QRScannerError . open_settings_unavailable)
488
- }
489
462
}
490
463
}
491
464
}
0 commit comments