Zxing.net.mobile: App crashes when clicking on scan button

Created on 15 Jun 2018  路  2Comments  路  Source: Redth/ZXing.Net.Mobile

Hi there,

I am running a Xamarin Forms .NET Standard project with the following versions:

Xamarin Forms : 3.0.0.561731
.NET Standard Library : v2.0.1
ZXing.Net.Mobile.Forms : 2.4.1
Testing on iPhone 6, iOS 11.4

The project is also available on my github at this location.

After reading a lot of articles and the documentation, I have implemented ZXing as follows:

Core Project

Services/IBarcodeScannerService.cs
public interface IBarcodeScannerService
{
Task<string> ScanAsync();
}

MainPage.xaml
<StackLayout>
<!-- Place new controls here -->
<Button x:Name="BttnScan" Text="Scan" HorizontalOptions="Center"
VerticalOptions="CenterAndExpand" Clicked="BttnScan_Clicked" />
<Label x:Name="TxtScanResults" Text="Welcome to Xamarin.Forms!" HorizontalOptions="Center"
VerticalOptions="CenterAndExpand" />
</StackLayout>

MainPage.xaml.cs

public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
}

private async void BttnScan_Clicked(object sender, EventArgs e)
{
try
{
var scanner = DependencyService.Get<IBarcodeScannerService>();
var result = await scanner.ScanAsync();
if (result != null)
TxtScanResults.Text = result;
}
catch (Exception ex)
{
throw;
}
}
}

iOS Project

AppDelegate.cs
public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
{
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
global::Xamarin.Forms.Forms.Init();
ZXing.Net.Mobile.Forms.iOS.Platform.Init();
LoadApplication(new App());
return base.FinishedLaunching(app, options);
}

Services/BarcodeScannerService.cs
[assembly:Dependency(typeof(BarcodeApp.iOS.Services.BarcodeScannerService))]
namespace BarcodeApp.iOS.Services
{
public class BarcodeScannerService : IBarcodeScannerService
{
public async Task<string> ScanAsync()
{
var scanner = new MobileBarcodeScanner();
var scanResults = await scanner.Scan();
return (scanResults != null) ? scanResults.Text : string.Empty;
}
}
}

Every time I hit the 'Scan' button on the xaml page, the app crashes as it is about to launch the scanner. The output window on the VS spews out the following:

Loaded assembly: /private/var/containers/Bundle/Application/EEE6188F-D223-427B-99FC-19F78FE70005/BarcodeApp.iOS.app/ZXing.Net.Mobile.Forms.dll [External]
Loaded assembly: /private/var/containers/Bundle/Application/EEE6188F-D223-427B-99FC-19F78FE70005/BarcodeApp.iOS.app/ZXing.Net.Mobile.Forms.iOS.dll [External]
Loaded assembly: /private/var/containers/Bundle/Application/EEE6188F-D223-427B-99FC-19F78FE70005/BarcodeApp.iOS.app/Xamarin.Forms.Xaml.dll [External]
Loaded assembly: /private/var/containers/Bundle/Application/EEE6188F-D223-427B-99FC-19F78FE70005/BarcodeApp.iOS.app/BarcodeApp.dll
Loaded assembly: /private/var/containers/Bundle/Application/EEE6188F-D223-427B-99FC-19F78FE70005/BarcodeApp.iOS.app/BarcodeApp.iOS.exe
Thread started: #3
Thread started: #4
Thread started: #5
Thread started: #6
2018-06-15 19:21:23.988 BarcodeApp.iOS[760:940637] Starting to scan...
2018-06-15 19:21:24.053 BarcodeApp.iOS[760:940637] ZXingScannerView.Setup() took 49.335 ms.
2018-06-15 19:21:24.054 BarcodeApp.iOS[760:940637] StartScanning
2018-06-15 19:21:24.087 BarcodeApp.iOS[760:940668] critical:
Native stacktrace:
2018-06-15 19:21:24.088 BarcodeApp.iOS[760:940668] critical: 0 BarcodeApp.iOS 0x0000000101bbfaf8 BarcodeApp.iOS + 20216568
2018-06-15 19:21:24.088 BarcodeApp.iOS[760:940668] critical: 1 libsystem_platform.dylib 0x0000000182860b58 _sigtramp + 52
2018-06-15 19:21:24.088 BarcodeApp.iOS[760:940668] critical: 2 libsystem_kernel.dylib 0x00000001826be968 + 100
2018-06-15 19:21:24.088 BarcodeApp.iOS[760:940668] critical: 3 libsystem_kernel.dylib 0x00000001826be994 system_set_sfi_window + 0
2018-06-15 19:21:24.089 BarcodeApp.iOS[760:940668] critical: 4 TCC 0x000000018580a9b4 + 0
2018-06-15 19:21:24.089 BarcodeApp.iOS[760:940668] critical: 5 TCC 0x000000018580a8e8 + 0
2018-06-15 19:21:24.089 BarcodeApp.iOS[760:940668] critical: 6 TCC 0x000000018580e12c + 296
2018-06-15 19:21:24.089 BarcodeApp.iOS[760:940668] critical: 7 libxpc.dylib 0x000000018289e700 + 60
2018-06-15 19:21:24.089 BarcodeApp.iOS[760:940668] critical: 8 libxpc.dylib 0x000000018289e63c + 88
2018-06-15 19:21:24.089 BarcodeApp.iOS[760:940668] critical: 9 libdispatch.dylib 0x0000000182531178 + 16
2018-06-15 19:21:24.089 BarcodeApp.iOS[760:940668] critical: 10 libdispatch.dylib 0x0000000182548bec + 320
2018-06-15 19:21:24.089 BarcodeApp.iOS[760:940668] critical: 11 libdispatch.dylib 0x0000000182537a44 + 396
2018-06-15 19:21:24.089 BarcodeApp.iOS[760:940668] critical: 12 libdispatch.dylib 0x000000018253dcac + 588
2018-06-15 19:21:24.089 BarcodeApp.iOS[760:940668] critical: 13 libdispatch.dylib 0x000000018253d9fc + 120
2018-06-15 19:21:24.090 BarcodeApp.iOS[760:940668] critical: 14 libsystem_pthread.dylib 0x0000000182863fac _pthread_wqthread + 1176
2018-06-15 19:21:24.090 BarcodeApp.iOS[760:940668] critical: 15 libsystem_pthread.dylib 0x0000000182863b08 start_wqthread + 4

2018-06-15 19:21:24.090 BarcodeApp.iOS[760:940668] critical:

Got a SIGABRT while executing native code. This usually indicates
a fatal error in the mono runtime or one of the native libraries

used by your application.

The app has been terminated.
Failed to Stop app: An error occured on client IDB410101 while executing a reply for topic xvs/idb/4.10.10.1/stop-app
The app has been terminated.

I'm attaching a short video here as well
BarcodeErrorScreenRecording2.zip

Appreciate if I could get some help at the earliest.

Thanks
Noel

Most helpful comment

Ok!! I figured this out.

As per these links for iOS10 & iOS11, starting with iOS 10 onwards, you have to declare access to camera or the App will crash. This is done by adding 'NSCameraUsageDescription' key to Info.plist as follows.

<key>NSCameraUsageDescription</key>
<string>Require access to camera to scan barcodes</string>

It would be great if the ZXing documentation for iOS could contain this tip.

Cheers
Noel

All 2 comments

Ok!! I figured this out.

As per these links for iOS10 & iOS11, starting with iOS 10 onwards, you have to declare access to camera or the App will crash. This is done by adding 'NSCameraUsageDescription' key to Info.plist as follows.

<key>NSCameraUsageDescription</key>
<string>Require access to camera to scan barcodes</string>

It would be great if the ZXing documentation for iOS could contain this tip.

Cheers
Noel

Would it be possible for the project's readme.med to be updated to reflect the necessity of adding the NSCameraUsageDescription key to info.plist? I've tripped over this issue three times now (shame on me) and I know first time users have tripped over it multiple times.

Was this page helpful?
0 / 5 - 0 ratings