question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Regression: Torch not working on iOS

See original GitHub issue

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?

Code example

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

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();
    }
}

Version info

Xamarin.Forms 4.8.0.1269 ZXing.Net.Mobile 3.0.0-beta5 ZXing.Net.Mobile.Forms 3.0.0-beta5

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:2
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
LeroyStainescommented, Feb 2, 2022

I’ve created a pull request for a fix for this:

https://github.com/Redth/ZXing.Net.Mobile/pull/1028

2reactions
radioactivemancommented, Aug 18, 2020

@Softtinn: That’s the same workaround I am using meanwhile.

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);
    }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Shipping a Neural Network on iOS with CoreML, PyTorch ...
I am not an expert when it comes to mechanical watches, but the main question hovering over this topic is: Will a watch...
Read more >
TorchScript Error Unknown builtin op - Mobile
Hi there,. I am trying to use a torch script exported module traced on iOS device but I am getting this error:
Read more >
Why regression model built using torch wont work well?
I am quite new with torch. And I have mostly difficulty with fitting simple feed forward network with some random data class Net(torch.nn....
Read more >
15 Ways to Fix iPhone Flashlight Not Working in iOS 16
15 Ways to Fix iPhone Flashlight Not Working in iOS 16 · 1. Remove Any Cover · 2. Charge Your iPhone · 3....
Read more >
Core ML | Apple Developer Forums
Hi everyone, I'm developing an iOS app which uses a PyTorch GPT2 model converted to Core ML via the Python coremltools. When I...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found