Reputation: 549
I'm new to Ionic and Cordova. I need to create a plugin for ionic using Cordova and integrate it in sample ionic app.
Steps I Followed are:
Created a simple ionic plugin using plugman
plugman create --name SayHello --plugin_id cordova-plugin-sayhello -plugin_version 0.0.1
Added android platform to above plugin.
cd SayHello/ && plugman platform add --platform_name android
Now I want to integrate this plugin into my ionic app.
ionic cordova plugin add ../SayHello
In my ionic app inside Home.ts, I wrote this piece of code.
declare var cordova: any;
var success = function(result) {
console.log(result);
}
var failure = function(err) {
console.log(err);
}
cordova.plugins.HelloWorld.coolMethod("SayHelloTest", success, failure);
The problem is I cannot call any function from success or failure in the ionic app.
like if I call function doSomething from success:
var success = function(result) {
doSomething(result);
}
It Shows Error doSomething function not found. It can only print in console.
Upvotes: 2
Views: 1335
Reputation: 726
To install plugman, you must have node installed on your machine. Then you can run the following command from anywhere in your environment to install plugman globally, so that it is available from any directory:
$ npm install -g plugman
Once you have installed Plugman and have created a Cordova project, you can start adding plugins to the platform with:
$ plugman create --name SayHello --plugin_id cordova-plugin-sayhello -plugin_version 0.0.1
Upvotes: 0
Reputation: 460
After creating the plugin, it is theoretically possible to import it also with plugman in order to do what you're trying to do. I have read the command is:
plugman install --platform android --project projectPlatformPath --plugin pluginPath
Anyway, this didn't work for me when I tried and also makes your plugin uncomfortable to use. It is probably a better idea to create an ionic wrapper for your plugin with gulp
and copy it into your project's node_modules/@ionic-native
. This way you would be able to inject it like the other plugins you just add with ionic cordova plugin add cordova-plugin-name-here
. This is also the recommended way by Ionic.
Detailed instructions would be long to write here. Just visit this tutorial and follow the step-by-step instructions.
Upvotes: 0
Reputation: 29614
you need to create success as the class function and either send it as a bound function or call inside arrow.
declare var cordova:any;
class HomePage{
//constructor etc...
doSomething(res:any){
}
success(result){
this.doSomething(result);
}
failure(err){}
//..
//call
callCordovaFunction(){
cordova.plugins.HelloWorld.coolMethod("SayHelloTest", this.success.bind(this), this.failure.bind(this));
//or
cordova.plugins.HelloWorld.coolMethod("SayHelloTest", (res)=>this.success(res),(err)=>this.failure(err));
}
}
Upvotes: 1