Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,10 @@ packages:
dependency: transitive
description:
name: meta
sha256: e3641ec5d63ebf0d9b41bd43201a66e3fc79a65db5f61fc181f04cd27aab950c
sha256: "23f08335362185a5ea2ad3a4e597f1375e78bce8a040df5c600c8d3552ef2394"
url: "https://pub.dev"
source: hosted
version: "1.16.0"
version: "1.17.0"
path:
dependency: transitive
description:
Expand Down Expand Up @@ -264,10 +264,10 @@ packages:
dependency: transitive
description:
name: test_api
sha256: "522f00f556e73044315fa4585ec3270f1808a4b186c936e612cab0b565ff1e00"
sha256: ab2726c1a94d3176a45960b6234466ec367179b87dd74f1611adb1f3b5fb9d55
url: "https://pub.dev"
source: hosted
version: "0.7.6"
version: "0.7.7"
vector_math:
dependency: transitive
description:
Expand Down
40 changes: 24 additions & 16 deletions ios/Classes/SwiftFlutterBarcodeScannerPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ let ONLY_BARCODE = [
AVMetadataObject.ObjectType.upce];

public class SwiftFlutterBarcodeScannerPlugin: NSObject, FlutterPlugin, ScanBarcodeDelegate,FlutterStreamHandler {

public static var viewController = UIViewController()

public static var lineColor:String=""
public static var cancelButtonText:String=""
public static var isShowFlashIcon:Bool=false
Expand All @@ -59,9 +58,18 @@ public class SwiftFlutterBarcodeScannerPlugin: NSObject, FlutterPlugin, ScanBarc
public static var delayMillis:Int=0
public static var delayTimer: Timer?

/// Get the root view controller safely
public static var viewController: UIViewController? {
if let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene,
let rootVC = windowScene.windows.first?.rootViewController {
return rootVC
}
// Fallback for older iOS versions
return UIApplication.shared.delegate?.window??.rootViewController
}


public static func register(with registrar: FlutterPluginRegistrar) {
viewController = (UIApplication.shared.delegate?.window??.rootViewController)!
let channel = FlutterMethodChannel(name: "flutter_barcode_scanner", binaryMessenger: registrar.messenger())
let instance = SwiftFlutterBarcodeScannerPlugin()
registrar.addMethodCallDelegate(instance, channel: channel)
Expand Down Expand Up @@ -155,30 +163,29 @@ public class SwiftFlutterBarcodeScannerPlugin: NSObject, FlutterPlugin, ScanBarc
controller.modalPresentationStyle = .fullScreen
}

guard let viewController = SwiftFlutterBarcodeScannerPlugin.viewController else {
result("-1")
return
}

if checkCameraAvailability(){
if checkForCameraPermission() {
SwiftFlutterBarcodeScannerPlugin.viewController.present(controller
, animated: true) {

}
viewController.present(controller, animated: true)
}else {
AVCaptureDevice.requestAccess(for: .video) { success in
DispatchQueue.main.async {
if success {
SwiftFlutterBarcodeScannerPlugin.viewController.present(controller
, animated: true) {

}
viewController.present(controller, animated: true)
} else {
let alert = UIAlertController(title: "Action needed", message: "Please grant camera permission to use barcode scanner", preferredStyle: .alert)

alert.addAction(UIAlertAction(title: "Grant", style: .default, handler: { action in
UIApplication.shared.open(URL(string: UIApplication.openSettingsURLString)!)
}))

alert.addAction(UIAlertAction(title: "Cancel", style: .cancel))
SwiftFlutterBarcodeScannerPlugin.viewController.present(alert, animated: true)

viewController.present(alert, animated: true)
}
}
}}
Expand All @@ -193,10 +200,11 @@ public class SwiftFlutterBarcodeScannerPlugin: NSObject, FlutterPlugin, ScanBarc

/// Show common alert dialog
func showAlertDialog(title:String,message:String){
guard let viewController = SwiftFlutterBarcodeScannerPlugin.viewController else { return }
let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
let alertAction = UIAlertAction(title: "Ok", style: .default, handler: nil)
alertController.addAction(alertAction)
SwiftFlutterBarcodeScannerPlugin.viewController.present(alertController, animated: true, completion: nil)
viewController.present(alertController, animated: true, completion: nil)
}
}

Expand Down