From a25146d136fb4fcafdd41b5c23fbb60e3a5284f9 Mon Sep 17 00:00:00 2001 From: Jonathan Dick Date: Fri, 13 Mar 2020 13:30:58 -0400 Subject: [PATCH] Clean up code for current view controller This might actually help an oldie but a goodie and fix #138 --- .../iOS/MobileBarcodeScanner.ios.cs | 29 +++++-------------- 1 file changed, 8 insertions(+), 21 deletions(-) diff --git a/ZXing.Net.Mobile/iOS/MobileBarcodeScanner.ios.cs b/ZXing.Net.Mobile/iOS/MobileBarcodeScanner.ios.cs index 09adb58ac..d323eb499 100644 --- a/ZXing.Net.Mobile/iOS/MobileBarcodeScanner.ios.cs +++ b/ZXing.Net.Mobile/iOS/MobileBarcodeScanner.ios.cs @@ -11,24 +11,14 @@ namespace ZXing.Mobile public partial class MobileBarcodeScanner : MobileBarcodeScannerBase { IScannerViewController viewController; - - WeakReference weakAppController; - ManualResetEvent scanResultResetEvent = new ManualResetEvent(false); + readonly WeakReference weakAppController; + readonly ManualResetEvent scanResultResetEvent = new ManualResetEvent(false); public MobileBarcodeScanner(UIViewController delegateController) => weakAppController = new WeakReference(delegateController); public MobileBarcodeScanner() - { - foreach (var window in UIApplication.SharedApplication.Windows) - { - if (window.RootViewController != null) - { - weakAppController = new WeakReference(window.RootViewController); - break; - } - } - } + => weakAppController = new WeakReference(Xamarin.Essentials.Platform.GetCurrentUIViewController()); public Task Scan(bool useAVCaptureEngine) => Scan(new MobileBarcodeScanningOptions(), useAVCaptureEngine); @@ -56,12 +46,11 @@ void InternalScanContinuously(MobileBarcodeScanningOptions options, bool useAVCa if (useAVCaptureEngine) allRequestedFormatsSupported = AVCaptureScannerView.SupportsAllRequestedBarcodeFormats(options.PossibleFormats); - UIViewController appController; - if (weakAppController.TryGetTarget(out appController)) + if (weakAppController.TryGetTarget(out var appController)) { var tcs = new TaskCompletionSource(); - appController.InvokeOnMainThread(() => + appController?.InvokeOnMainThread(() => { if (useAVCaptureEngine && is7orgreater && allRequestedFormatsSupported) { @@ -93,7 +82,7 @@ void InternalScanContinuously(MobileBarcodeScanningOptions options, bool useAVCa scanHandler(barcodeResult); }; - appController.PresentViewController((UIViewController)viewController, true, null); + appController?.PresentViewController((UIViewController)viewController, true, null); }); } } @@ -122,10 +111,8 @@ void InternalScanContinuously(MobileBarcodeScanningOptions options, bool useAVCa if (weakAppController.TryGetTarget(out var appController)) { - appController.InvokeOnMainThread(() => + appController?.InvokeOnMainThread(() => { - - if (useAVCaptureEngine && is7orgreater && allRequestedFormatsSupported) { viewController = new AVCaptureScannerViewController(options, this); @@ -167,7 +154,7 @@ void InternalScanContinuously(MobileBarcodeScanningOptions options, bool useAVCa }); }; - appController.PresentViewController((UIViewController)viewController, true, null); + appController?.PresentViewController((UIViewController)viewController, true, null); }); }