@@ -3,10 +3,10 @@ import AVFoundation
33
44@objc ( QRScanner)
55class QRScanner : CDVPlugin , AVCaptureMetadataOutputObjectsDelegate {
6-
6+
77 class CameraView : UIView {
88 var videoPreviewLayer : AVCaptureVideoPreviewLayer ?
9-
9+
1010 func interfaceOrientationToVideoOrientation( _ orientation : UIInterfaceOrientation ) -> AVCaptureVideoOrientation {
1111 switch ( orientation) {
1212 case UIInterfaceOrientation . portrait:
@@ -29,18 +29,18 @@ class QRScanner : CDVPlugin, AVCaptureMetadataOutputObjectsDelegate {
2929 layer. frame = self . bounds;
3030 }
3131 }
32-
32+
3333 self . videoPreviewLayer? . connection? . videoOrientation = interfaceOrientationToVideoOrientation ( UIApplication . shared. statusBarOrientation) ;
3434 }
35-
36-
35+
36+
3737 func addPreviewLayer( _ previewLayer: AVCaptureVideoPreviewLayer ? ) {
3838 previewLayer!. videoGravity = AVLayerVideoGravity . resizeAspectFill
3939 previewLayer!. frame = self . bounds
4040 self . layer. addSublayer ( previewLayer!)
4141 self . videoPreviewLayer = previewLayer;
4242 }
43-
43+
4444 func removePreviewLayer( ) {
4545 if self . videoPreviewLayer != nil {
4646 self . videoPreviewLayer!. removeFromSuperlayer ( )
@@ -98,24 +98,14 @@ class QRScanner : CDVPlugin, AVCaptureMetadataOutputObjectsDelegate {
9898
9999 // utility method
100100 @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 ) {
115103 background!( )
116104 }
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+ }
119109 }
120110 }
121111 }
@@ -133,15 +123,8 @@ class QRScanner : CDVPlugin, AVCaptureMetadataOutputObjectsDelegate {
133123 if ( captureSession? . isRunning != true ) {
134124 cameraView. backgroundColor = UIColor . clear
135125 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)
145128 // older iPods have no back camera
146129 if ( backCamera == nil ) {
147130 currentCamera = 1
@@ -467,8 +450,7 @@ class QRScanner : CDVPlugin, AVCaptureMetadataOutputObjectsDelegate {
467450 }
468451
469452 @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 {
472454 return
473455 }
474456 if UIApplication . shared. canOpenURL ( settingsUrl) {
@@ -477,15 +459,6 @@ class QRScanner : CDVPlugin, AVCaptureMetadataOutputObjectsDelegate {
477459 } )
478460 } else {
479461 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- }
489462 }
490463 }
491464}
0 commit comments