8

Has anyone managed to get the BarcodeScanning plugin for PhoneGap to work on PhoneGap 1.7.0?

Barcode Scanning plugin: https://github.com/phonegap/phonegap-plugins/tree/master/iOS/BarcodeScanner

The issues is that the plugin is not getting set when its added..

I get the following when I call "alert(window.plugins.barcodeScanner);"

"undefined"

I am trying to isolate the point where the plugin is failing to get added and will update the question once I know more..

Thanks in advance for anyone who can help...


Updated answer below:

damien murphy.
  • 371
  • 2
  • 16

4 Answers4

11

Excellent,

The plugin now works again.
One issue is the documentation for the plugin still says the key in Cordova.plist should be org.apache.cordova.barcodeScanner but obvious thing now it should be com.cordova.barcodeScanner.

Mayur Birari
  • 5,837
  • 8
  • 34
  • 61
WTFJesus
  • 111
  • 3
6

OK so after a bit of poking around and using the twitter PhoneGap plugin as an example I managed to get it working!!

I used this as a basis for my approach because the lovely people at twitter updated their plugin to work with PhoneGap 1.7.0 thank god!!

Twitter PhoneGap Plugin: https://github.com/phonegap/phonegap-plugins/blob/master/iOS/Twitter/js/TwitterPlugin.js

Here is the updated barcodescanner.js code:

var BarcodeScanner = function(){};

BarcodeScanner.prototype.isBarcodeScannerAvailable = function(response){
    cordova.exec(response, null, "BarcodeScannerPlugin", "isBarcodeScannerAvailable", []);
};

BarcodeScanner.prototype.isBarcodeScannerSetup = function(response){
    cordova.exec(response, null, "BarcodeScannerPlugin", "isBarcodeScannerSetup", []);
};

//-------------------------------------------------------------------
BarcodeScanner.Encode = {
TEXT_TYPE:     "TEXT_TYPE",
EMAIL_TYPE:    "EMAIL_TYPE",
PHONE_TYPE:    "PHONE_TYPE",
SMS_TYPE:      "SMS_TYPE",
CONTACT_TYPE:  "CONTACT_TYPE",
LOCATION_TYPE: "LOCATION_TYPE"
}

//-------------------------------------------------------------------
BarcodeScanner.prototype.scan = function(success, fail, options) {
    function successWrapper(result) {
        result.cancelled = (result.cancelled == 1)
        success.call(null, result)
    }

    if (!fail) { fail = function() {}}

    if (typeof fail != "function")  {
        console.log("BarcodeScanner.scan failure: failure parameter not a function")
        return
    }

    if (typeof success != "function") {
        fail("success callback parameter must be a function")
        return
    }

    if ( null == options ) 
        options = []

        return PhoneGap.exec(successWrapper, fail, "com.cordova.barcodeScanner", "scan", options)
        }

//-------------------------------------------------------------------
BarcodeScanner.prototype.encode = function(type, data, success, fail, options) {
    if (!fail) { fail = function() {}}

    if (typeof fail != "function")  {
        console.log("BarcodeScanner.scan failure: failure parameter not a function")
        return
    }

    if (typeof success != "function") {
        fail("success callback parameter must be a function")
        return
    }

    return PhoneGap.exec(success, fail, "com.cordova.barcodeScanner", "encode", [{type: type, data: data, options: options}])
}

cordova.addConstructor(function() {

                       /* shim to work in 1.5 and 1.6  */
                       if (!window.Cordova) {
                       window.Cordova = cordova;
                       };


                       if(!window.plugins) window.plugins = {};
                       window.plugins.barcodeScanner = new BarcodeScanner();
                       });
damien murphy.
  • 371
  • 2
  • 16
  • thanks for pointing out the new name: com.cordova.barcodeScanner. How did you find this out?
    I changed it in Cordova.plist and at least it created the scanner object ok. Note that the 21 hour old file still referenced org.apache.cordova.barcodeScanner at https://github.com/phonegap/phonegap-plugins/blob/master/iOS/BarcodeScanner/barcodescanner.js.
    – GeorgeW Jul 05 '12 at 22:32
  • 1
    @damien murphy - thanks mate, finally after 3 days of hair pulling it works...;) – SJ Reddy Sep 14 '12 at 14:27
3

i just added barcodescanner to cordova 2.3 - it's quite simple

after copying the necessary files you only need to add the following line to config.xml

<plugin name="org.apache.cordova.barcodeScanner" value="CDVBarcodeScanner" /> 
0

In case this helps anyone: https://github.com/zeroasterisk/PhoneGap-BarcodeScanner-Example-iOS

Specifically:

Installed the plugin (in a handful of paths) but kept the one which worked. Implemented a basic JS scanner code to demonstrate functionality: auto-runs on load, auto-reloads on error, alerts on success/failure/cancel.

NOTE: the comments on the barcodescanner.js and index.js, both mention my customization of the define/require object paths. I could not get the demo/example paths to work, after several permutations.

zeroasterisk
  • 2,199
  • 1
  • 23
  • 28