Redth / ZXing.Net.Mobile

Barcode Scanner for Xamarin.iOS, Xamarin.Android, UWP and Tizen
MIT License
1.07k stars 701 forks source link

Xamarin.Forms iOS Torch does not work & Torch State not maintained after App Resume #994

Open DuncanMT opened 3 years ago

DuncanMT commented 3 years ago

⤵️ What is the current behaviour?

if (err != null) // Always False {


## 🆕 Suggestions

1. Add a RefreshTorch method to the Torch.Android.cs file and call that method on scan resume from `public void RefreshCamera()` in the `CameraAnalyzer.android.cs` file in order to restore the correct Torch state after app resume.

public void RefreshTorch() { if (IsEnabled) { IsEnabled = false; Enable(true); } }


2. Change `LockForConfiguration` call to this:

if (device.LockForConfiguration(out var err)) {


3. Add something like a `WillEnterForeground` observer to the `ZXingScannerView.ios.cs` file in order to restore the correct Torch state after app resume.  The following code is just an example, and only seem to work if you leave the scanner open for a bit before backgrounding, it doesnt work if you turn the torch on and instantyl background.  I will update here if I find something that does work in that case as well.

// Inside Setup method WillEnterForegroundObserver = NSNotificationCenter.DefaultCenter.AddObserver( UIApplication.WillEnterForegroundNotification, _ => { if (IsTorchOn) { torch = false; Torch(true); } });

// Inside StopScanning method if (WillEnterForegroundObserver != null) { NSNotificationCenter.DefaultCenter.RemoveObserver(WillEnterForegroundObserver); }



4.  Add the missing call to `global::ZXing.Net.Mobile.Forms.iOS.Platform.Init();` in the finished launching method of the Sample.Forms.iOS  project.

I have all these changes on a local branch however I do not have permissions to push for your review, from my inital developer testing these changes seem to resolve the issues for me on Android, and partially on iOS. 
kobynz commented 3 years ago

@Redth Any chances of getting a quick service update out to address this?

LeroyStaines commented 2 years ago

Change it to if (err == null) instead of if (err != null).

I've created a pull request for this: https://github.com/Redth/ZXing.Net.Mobile/pull/1028