Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MonoTouch, ZXing: Presenting ZXingScannerViewController failed

So in the beginning of my application users have to ability to scan a QR-code. In the app settings the user can scan another barcode to change some data within the settings.

In the beginning of my application scanner works just fine but when I try to scan a barcode within the settingsVC I get the following Warning:

Warning: Attempt to present ZXing.Mobile.ZXingScannerViewController: 0x18036dc0 on UINavigationController: 0x16d8afe0 whose view is not in the window hierarchy!

I already tried to invoke the scan on viewDidAppear but I get the same warning.

            button_ScanAPI.TouchUpInside += async (sender, e) => {
                var scanner = new ZXing.Mobile.MobileBarcodeScanner ();
                var result = await scanner.Scan ();

                if (result != null) {
                    textField_APIKey.Text = result.Text;
                }   
            };

EDIT:

Tried to use the barcode scanner without async but I still get the same msg.

var scanner = new ZXing.Mobile.MobileBarcodeScanner ();
                scanner.Scan (true).ContinueWith (t => {   
                    if (t.Result != null) {

                        InvokeOnMainThread (() => {
                            textField_APIKey.Text = t.Result.Text;

                        });
                    }
                });

And I also tried using AVFoundation resulting in the same error:

Warning: Attempt to present <AVCaptureScannerViewController: 0x16fb1d00> on <UINavigationController: 0x16ebe790> whose view is not in the window hierarchy!

EDIT2:

This is a part of the flow within my app.

enter image description here

like image 306
Florian Schaal Avatar asked Dec 22 '13 16:12

Florian Schaal


2 Answers

I think that by default the ZXing library must be looking to your most top level NavigationController and tries to present the modal view controller from there. Like you, I have modally presented another NavigationController over the top of the root one. I was able to fix this by changing the constructor to:

var scanner = new MobileBarcodeScanner (this);
var result = await scanner.Scan ();

where "this" is the ViewController from which you are actually calling the scanner.

like image 77
tom-pratt Avatar answered Nov 10 '22 01:11

tom-pratt


So, you can scan QR natively. In iOS 7 AVFoundation is capable to scan QR. Take a look to the doc.

And here there is an example using Xamarin.

like image 25
dcorbatta Avatar answered Nov 09 '22 23:11

dcorbatta