Search code examples
ioscordovaphonegap-pluginsbarcode-scanner

Can't use Barcode scanner in Cordova (plugin is installed).


I'm having a problem with my Cordova app when trying to use a barcode scanner plugin.

The plugin seems to be installed correctly, since when I type in cordova plugin ls I get the response:

    [ 'com.phonegap.plugins.barcodescanner',
  'org.apache.cordova.console' ]

However I tried multiple ways of triggering the scanner but none worked. I also copied the barcodescanner.js into my www folder and in my index.html even if I read it's not necessary anymore. The suggested way in the read-me file is:

cordova.plugins.barcodeScanner.scan()

which didn't work out. I tried numerous other things but none of them worked. Now I'm hoping someone here encountered the same issue and found a solution. I'm testing the iOS app version.


Solution

  • Add this to your config.xml

    <feature name="BarcodeScanner">
        <param name="android-package" value="com.phonegap.plugins.barcodescanner.BarcodeScanner" />
    </feature>
    

    and while using the plugin

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