mardi 7 novembre 2017

Calling Cordova Plugin from Ember Controller

I am trying to create a cordova plugin for android which I can call from an Ember controller.

I created this simple plugin for testing this out using the command: cordova plugin add TestPlugin which I used as a stub to build this on.

When the controller runs the action that contains the call it doesn't seem to fire as there is no alert when running the app to show a success or failure response. I am testing using an android emulator.

I've seen many examples of where this has been implemented and works for cordova apps but nowhere within an Ember-Cordova app.

Perhaps I'm missing a reference from within my controller to be able to use cordova.exec? If so, what could this possibly be? Your help is greatly appreciated. Let me know if any further detail is required.

TestPlugin.java

package cordova.plugin.test.plugin;

import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CallbackContext;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

public class TestPlugin extends CordovaPlugin {

    public static String TAG = "TestPlugin";

    @Override
    public boolean execute(String action, JSONArray args,
        CallbackContext callbackContext)
    throws JSONException {
        Log.d(TAG, "called execute on TestPlugin");
        Log.d(TAG, "action is: " + action);
        if (action.equals("sayHello")) {
            this.sayHello(args.getString(0), callbackContext);
            return true;
        } else {
            Log.d(TAG, "Did not match any registered actions");
            callbackContext.error("Invalid action: " + action);
            return false;
        }
    }

    private void sayHello(string name, CallbackContext callbackContext) {
        try {
            String responseText = "Hello world, " + name;
            callbackContext.success(responseText);
        } catch (JSONException e) {
            callbackContext.error("Failed to parse parameters.");
        }
    }
}

config.xml

<feature name="TestPlugin">
    <param name="android-package" value="cordova.plugin.test.plugin.TestPlugin" />
    <param name="onload" value="true" />
</feature>

Controller

cordova.exec(function(successData){
    alert("OK: " + successData);
}, function(failureData){
    alert("FAIL: " + failureData);
},
"TestPlugin",
"sayHello",
["Oliver"]);




Aucun commentaire:

Enregistrer un commentaire