Ahmet Demir Ahmet Demir - 6 months ago 385
AngularJS Question

Ionic BarcodeScanner does not work on ios

I created an ionic app, it is work on android good. But the barcode scanner is not working correct in ios.

My code;

$cordovaBarcodeScanner.scan().then(function (barcodeData) {
console.log("Data : "+barcodeData.text);
});


But XCODE giving me something like that as non-stop;
enter image description here

When I tried to this;

cordova.plugins.barcodeScanner.scan(
function (result) {
alert("We got a barcode\n" +
"Result: " + result.text + "\n" +
"Format: " + result.format + "\n" +
"Cancelled: " + result.cancelled);
},
function (error) {
alert("Scanning failed: " + error);
},
{
"preferFrontCamera" : true, // iOS and Android
"showFlipCameraButton" : true, // iOS and Android
"prompt" : "Place a barcode inside the scan area", // supported on Android only
"formats" : "QR_CODE,PDF_417", // default: all but PDF_417 and RSS_EXPANDED
"orientation" : "landscape" // Android only (portrait|landscape), default unset so it rotates with the device
}
};


getting this error:

Warning: Attempt to present <CDVbcsViewController: 0x15f30c400> on <MainViewController: 0x15dd4fab0> whose view is not in the window hierarchy!

Answer

By doing this i'm able to scan QR code in iOS.

  • Add iOS Platform:

    ionic platform add ios
    
  • Install ngCordova

    bower install ngCordova
    
  • Added Barcodescanner plugin like this:

    cordova plugin add https://github.com/phonegap/phonegap-plugin-barcodescanner.git
    
  • index.html

    <button class="button button-block button-positive" ng-click="scanBarcode()">
          <i class="icon ion-qr-scanner"></i>
          Scan Now
    </button>
    
  • app.js

    angular.module('myApp', ['ionic','ngCordova']) // include ngCordova
    
    // Scan 
    angular.module("myApp").controller('scanner',function($scope, $cordovaBarcodeScanner) {
    $scope.scanBarcode = function() {
    $cordovaBarcodeScanner.scan().then(function(imageData) {
        alert(imageData.text);
        console.log("Barcode Format -> " + imageData.format);
        console.log("Cancelled -> " + imageData.cancelled);
    }, function(error) {
        console.log("An error happened -> " + error);
    });
    };
    });
    

My nodejs version : v5.5.0

Phonegap version : 6.0.2

Ionic version : 1.7.14