Enabling the torch does not work here, using an iPhone 6 with iOS 12.4.8.
The same code (example below) works fine on Android.
In general the flash light is definitely working on the device. I have also tried Xamarin.Essentials.FlashLight with success.
Can anyone reproduce this issue?
ScannerPage.xaml
<ContentPage.Content>
<Grid>
<zxing:ZXingScannerView x:Name="scannerView"
IsAnalyzing="{Binding IsAnalyzing}" />
<zxing:ZXingDefaultOverlay x:Name="scannerOverlay"
TopText="{x:Static resx:AppResources.ScannerTopText}"
BottomText="{x:Static resx:AppResources.ScannerBottomText}"
ShowFlashButton="True" />
</Grid>
</ContentPage.Content>
ScanPage.xaml.cs
```c#
using System;
using Xamarin.Forms;
using ZXing;
using ZXing.Mobile;
public partial class ScanPage
{
public ScanPage()
{
this.InitializeComponent();
this.InitializeScannerView();
}
protected override void OnAppearing()
{
base.OnAppearing();
this.scannerView.IsScanning = true;
}
protected override void OnDisappearing()
{
this.scannerView.IsScanning = false;
base.OnDisappearing();
}
private void InitializeScannerView()
{
this.scannerView.Options = new MobileBarcodeScanningOptions
{
PossibleFormats = new[] { BarcodeFormat.QR_CODE }
};
this.scannerView.OnScanResult += this.ScannerViewOnOnScanResult;
this.scannerOverlay.FlashButtonClicked += this.ScannerOverlayOnFlashButtonClicked;
}
private async void ScannerViewOnOnScanResult(Result result)
{
if (this.ViewModel.ScanResultCommand.CanExecute(result))
{
await this.ViewModel.ScanResultCommand.ExecuteAsync(result);
}
}
private void ScannerOverlayOnFlashButtonClicked(Button sender, EventArgs eventArgs)
{
this.scannerView.ToggleTorch();
}
}
```
Xamarin.Forms 4.8.0.1269
ZXing.Net.Mobile 3.0.0-beta5
ZXing.Net.Mobile.Forms 3.0.0-beta5
@radioactiveman
We also have this exact same problem. Working fine on our android devices but not on our IPhone 8.
We are using the same versions as you are.
@radioactiveman
Actually, we found a workaround that might solve this problem for now. It seems that you cannot use Xamarin.Essentials Flashlight on android together with ZXing (this will cause an exception to be thrown). However, this doesn't seem to be the case on iOS(!!).
So we managed to get this working on both platforms with the following check:
if (Device.RuntimePlatform == Device.iOS)
await ToggleFlashIos();
else
zxing.ToggleTorch();
@Softtinn: That's the same workaround I am using meanwhile.
```c#
using Xamarin.Essentials;
...
private bool isTorchOn;
private async void ScannerOverlayOnFlashButtonClicked(Button sender, EventArgs eventArgs)
{
if (Device.RuntimePlatform == Device.iOS)
{
// Workaround for torch not working on iOS.
// Reported upstream as https://github.com/Redth/ZXing.Net.Mobile/issues/929
await this.ToggleTorchOniOS();
}
else
{
this.scannerView.ToggleTorch();
}
}
private async Task ToggleTorchOniOS()
{
try
{
if (this.isTorchOn)
{
await Flashlight.TurnOffAsync();
}
else
{
await Flashlight.TurnOnAsync();
}
this.isTorchOn = !this.isTorchOn;
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex);
}
}
```
Just a comment: ToggleTorch() is working in latest stable version: 2.4.1. It is not working in the 3.0.0-beta5. But this prerelease is needed, when you want to support Android SDK 29.
Just a comment: ToggleTorch() is working in latest stable version: 2.4.1. It is not working in the 3.0.0-beta5.
Hey @radioactiveman can you confirm this? I mean, is this bug a regression in v3.x but doesn't exist in 2.4.1 right?
Hey @radioactiveman can you confirm this? I mean, is this bug a regression in v3.x but doesn't exist in 2.4.1 right?
Yes, this is correct.
Ok thanks, then can you rename the title of this bug to have "Regression: " as a prefix? for visibility
Most helpful comment
@Softtinn: That's the same workaround I am using meanwhile.
```c#
using Xamarin.Essentials;
...
private bool isTorchOn;
private async void ScannerOverlayOnFlashButtonClicked(Button sender, EventArgs eventArgs)
{
if (Device.RuntimePlatform == Device.iOS)
{
// Workaround for torch not working on iOS.
// Reported upstream as https://github.com/Redth/ZXing.Net.Mobile/issues/929
await this.ToggleTorchOniOS();
}
else
{
this.scannerView.ToggleTorch();
}
}
private async Task ToggleTorchOniOS()
{
try
{
if (this.isTorchOn)
{
await Flashlight.TurnOffAsync();
}
else
{
await Flashlight.TurnOnAsync();
}
}
```