Skip to content

Commit

Permalink
ios fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobg committed Nov 28, 2021
1 parent 02b0028 commit 092c3e5
Showing 1 changed file with 15 additions and 42 deletions.
57 changes: 15 additions & 42 deletions src/ios/QRScanner.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import AVFoundation

@objc(QRScanner)
class QRScanner : CDVPlugin, AVCaptureMetadataOutputObjectsDelegate {

class CameraView: UIView {
var videoPreviewLayer:AVCaptureVideoPreviewLayer?

func interfaceOrientationToVideoOrientation(_ orientation : UIInterfaceOrientation) -> AVCaptureVideoOrientation {
switch (orientation) {
case UIInterfaceOrientation.portrait:
Expand All @@ -29,18 +29,18 @@ class QRScanner : CDVPlugin, AVCaptureMetadataOutputObjectsDelegate {
layer.frame = self.bounds;
}
}

self.videoPreviewLayer?.connection?.videoOrientation = interfaceOrientationToVideoOrientation(UIApplication.shared.statusBarOrientation);
}


func addPreviewLayer(_ previewLayer:AVCaptureVideoPreviewLayer?) {
previewLayer!.videoGravity = AVLayerVideoGravity.resizeAspectFill
previewLayer!.frame = self.bounds
self.layer.addSublayer(previewLayer!)
self.videoPreviewLayer = previewLayer;
}

func removePreviewLayer() {
if self.videoPreviewLayer != nil {
self.videoPreviewLayer!.removeFromSuperlayer()
Expand Down Expand Up @@ -98,24 +98,14 @@ class QRScanner : CDVPlugin, AVCaptureMetadataOutputObjectsDelegate {

// utility method
@objc func backgroundThread(delay: Double = 0.0, background: (() -> Void)? = nil, completion: (() -> Void)? = nil) {
if #available(iOS 8.0, *) {
DispatchQueue.global(qos: DispatchQoS.QoSClass.userInitiated).async {
if (background != nil) {
background!()
}
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + delay * Double(NSEC_PER_SEC)) {
if(completion != nil){
completion!()
}
}
}
} else {
// Fallback for iOS < 8.0
if(background != nil){
DispatchQueue.global(qos: DispatchQoS.QoSClass.userInitiated).async {
if (background != nil) {
background!()
}
if(completion != nil){
completion!()
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + delay * Double(NSEC_PER_SEC)) {
if(completion != nil){
completion!()
}
}
}
}
Expand All @@ -133,15 +123,8 @@ class QRScanner : CDVPlugin, AVCaptureMetadataOutputObjectsDelegate {
if (captureSession?.isRunning != true){
cameraView.backgroundColor = UIColor.clear
self.webView!.superview!.insertSubview(cameraView, belowSubview: self.webView!)
let availableVideoDevices = AVCaptureDevice.devices(for: AVMediaType.video)
for device in availableVideoDevices {
if device.position == AVCaptureDevice.Position.back {
backCamera = device
}
else if device.position == AVCaptureDevice.Position.front {
frontCamera = device
}
}
backCamera = AVCaptureDevice.default(.builtInWideAngleCamera, for: .video, position: .back)
frontCamera = AVCaptureDevice.default(.builtInWideAngleCamera, for: .video, position: .front)
// older iPods have no back camera
if(backCamera == nil){
currentCamera = 1
Expand Down Expand Up @@ -467,8 +450,7 @@ class QRScanner : CDVPlugin, AVCaptureMetadataOutputObjectsDelegate {
}

@objc func openSettings(_ command: CDVInvokedUrlCommand) {
if #available(iOS 10.0, *) {
guard let settingsUrl = URL(string: UIApplicationOpenSettingsURLString) else {
guard let settingsUrl = URL(string: UIApplication.openSettingsURLString) else {
return
}
if UIApplication.shared.canOpenURL(settingsUrl) {
Expand All @@ -477,15 +459,6 @@ class QRScanner : CDVPlugin, AVCaptureMetadataOutputObjectsDelegate {
})
} else {
self.sendErrorCode(command: command, error: QRScannerError.open_settings_unavailable)
}
} else {
// pre iOS 10.0
if #available(iOS 8.0, *) {
UIApplication.shared.openURL(NSURL(string: UIApplicationOpenSettingsURLString)! as URL)
self.getStatus(command)
} else {
self.sendErrorCode(command: command, error: QRScannerError.open_settings_unavailable)
}
}
}
}

0 comments on commit 092c3e5

Please sign in to comment.