How to add and use a (custom) cordova plugin in an Ionic project


#1

I would like write a cordova plugin and use it in an Ionic project.

However I just cannot figure out how to actually use it. I simply wrote the echo plugin example (fromt the phonegap tutorial website) and would like to output the return of the function in an alert.

I added the plugin with cordova plugin add <pathToDirectory>

my Echo.js file has a window.echo function.

But I can’t figure out how to use it in my app.js file.

because simply
window.echo("echotest", function(result) { alert(result); });

doesn’t work.
(I wrote that in the .run() function; that got generated with the tabs example app)

I tried other usages but I always can see the error in in adb logcat
Unknown provider: EchoProvider <- Echo


#2

I don’t know much about writing custom cordova plugins, but have you tried the window.echo call in the .run function or in the $platform.ready(function(){ window.echo() }); which is meant to fire when cordova is initalized. You should try in the last one.

I usually bind to it in the .run method, so it would be something like this:

   angular.module( 'starterapp', []).run(function(){
       $ionicPlatform.ready(function(){
           window.echo("echotest", function(result) {
              alert(result)
           });
       });
    });

#3

Can I test my custom ios plugin in the web browser ? I am returning a string from native objective-c to the javascript interface .


#4

No, you can’t test ur custom ios plugin from web browser as far as I know. You can maybe test it with the xcode simulator, but that is not the web browser.