I need a smartphone based Barcode/QRcode scanner app for my company and it has to work on iPhone and Android, but mostly iPhone. I decided to give Xamarin Forms a shot and it worked perfectly for Android, but took some serious legwork to get the app to even show up on an iPhone. I finally got it to open on the iPhone, but when I click on the scan button it throws up
Unhandled Exception:
System.NullReferenceException: Object reference not set to an instance of an
object`
in the Application class on UIApplication.Main(args, null, "AppDelegate");
using System;
using System.Collections.Generic;
using System.Linq;
using Foundation;
using UIKit;
namespace QRNatives.iOS
{
public class Application
{
// This is the main entry point of the application.
static void Main(string[] args)
{
// if you want to use a different Application Delegate class from "AppDelegate"
// you can specify it here.
UIApplication.Main(args, null, "AppDelegate");
}
}
}
I haven't been programming for too long, but it seems that normally null reference errors are fairly easy to trace. However, I can not see where the problem is actually happening with this one. I suspect the problem is in AppDelegate.cs, though nothing really sticks out to me.
using Foundation;
using UIKit;
namespace QRNatives.iOS
{
// The UIApplicationDelegate for the application. This class is responsible for launching the
// User Interface of the application, as well as listening (and optionally responding) to
// application events from iOS.
[Register("AppDelegate")]
public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
{
//
// This method is invoked when the application has loaded and is ready to run. In this
// method you should instantiate the window, load the UI into it and then make the window
// visible.
//
// You have 17 seconds to return from this method, or iOS will terminate your application.
//
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
global::Xamarin.Forms.Forms.Init();
global::ZXing.Net.Mobile.Forms.iOS.Platform.Init();
LoadApplication(new App());
return base.FinishedLaunching(app, options);
}
}
}
Just for more info, here is what I am using for the QrScanningService.cs for linking the iOS to the shared interface data.
using System.Threading.Tasks;
using QRNatives.Services;
using Xamarin.Forms;
using ZXing.Mobile;
[assembly: Dependency(typeof(QRNatives.iOS.Services.QrScanningService))]
namespace QRNatives.iOS.Services
{
class QrScanningService
{
public class QrCodeScanningService : IQrScanningServies
{
public async Task<string> ScanAsync()
{
var scanner = new MobileBarcodeScanner();
var scanResults = await scanner.Scan();
return scanResults.Text;
}
}
}
}
I have searched a number of old posts were this issue came up, but none of the ones I've read had any real answers to the problem. Has anyone had any experience successfully using ZXing with Xamarin for iOS? Or does anyone have any suggestions about how else I could track down where the error is actually coming from? I've been stuck on this for a while so any help would be greatly appreciated. Thanks.
To access camera
in iPhone, you should add permission in info.plist
:
<key>NSCameraUsageDescription</key>
<string>Camera acess to scan barcodes</string>
To edit info.plist
inside Visual Studio:
Right click info.plist
and choose open with
, you can choose Generic PList Editor
or XML editor
(whatever you want) to edit it.
More permissions:
Refer to : infoplist-reference