diff --git a/example/pubspec.lock b/example/pubspec.lock index 6d82933..0ce2a21 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -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: @@ -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: diff --git a/ios/Classes/SwiftFlutterBarcodeScannerPlugin.swift b/ios/Classes/SwiftFlutterBarcodeScannerPlugin.swift index 4087678..72cb241 100644 --- a/ios/Classes/SwiftFlutterBarcodeScannerPlugin.swift +++ b/ios/Classes/SwiftFlutterBarcodeScannerPlugin.swift @@ -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 @@ -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) @@ -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) } } }} @@ -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) } }