Unable to make custom plugin work [Ionic-Angular-Cordova]

387 views
Skip to first unread message

Andrea Castellini

unread,
Feb 15, 2016, 11:43:48 AM2/15/16
to phonegap
Hi all,

I'm trying to create a custom sample cordova plugin in my ionic+angular app but I'm not able to make it work.

I've added the feature in config.xml in the xcode root

<feature name="sayHelloPlugin">
 
<param name="ios-package" value="sayHelloPlugin" onload="true" />
</feature>

Then I added the cordova call function in my controller
cordova.exec(
 
function(data){
    alert
(data)
 
},
 
function(){
    alert
("Errore")
 
},
 
"SayHelloPlugin",
 
"sayHello",
 
["Andrea"]
);

And finally I've created the SayHelloPlugin class in the classes folder of xcode

SayHelloPlugin.h

#import <Foundation/Foundation.h>
#import <Cordova/CDV.h>

@interface SayHelloPlugin : CDVPlugin
- (void)sayHello:(CDVInvokedUrlCommand*)command;
@end

SayHelloPlugin.m
#import "SayHelloPlugin.h"

@implementation SayHelloPlugin
- (void)sayHello:(CDVInvokedUrlCommand*)command {
   
NSString *responseString = [NSString stringWithFormat:@"Hello %@", [command.arguments objectAtIndex:0]];
   
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:responseString];
   
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
@end

But when I try to call the plugin it throws an error ERROR: Plugin 'SayHelloPlugin' not found, or is not a CDVPlugin. Check your plugin mapping in config.xml.

I've already tried to wrap the cordova.exec in $ionicPlatform.ready() function, I've tried to remove and re-add the ios platform, I tried to create a plugin.xml in the project root without success.



Kerri Shotts

unread,
Feb 15, 2016, 12:38:20 PM2/15/16
to phonegap
When I'm developing a plugin, this is the workflow I use:

1. Create a separate plugin directory (and source repository, ideally):

mkdir -p /your/plugin/directory
cd /your/plugin/directory

2. Create your plugin's plugin.xml file. I shamelessly copy from the docs or core plugins and then modify as necessary. You can use the Device plugin for a start. Docs are at: https://cordova.apache.org/docs/en/dev/guide/hybrid/plugins/index.html

3. Add your native and www source files:

A typical structure looks like this:

plugin-root/
plugin.xml
readme.md
www/
your-js-files.js
src/
ios/
your-plugin.h
your-plugin.m
android/
your-plugin.java

These files don't have to have a lot in them to start -- you can stub them out, or copy from a core plugin again. Make sure your plugin.xml references these files, though, or adding the plugin won't do anything.

4. Create a test application for development:

cordova create plugin-test com.example.plugin-test plugin-test

5. Add the plugin to the project, but use --link so that changes you make in your IDE are maintained:

cordova plugin add /path/to/your/plugin --link

(Alternatively, you can just add the plugin regularly, and remember to copy any changes you make in the IDE back to your plugin source files. That works too, assuming you remember to do it!)

6. Add any necessary platforms:

cordova platform add ios...

7. Build once

cordova build

(Since you have a hello-world cordova project for testing, and are using stubs/core plugin files for your plugin, this shouldn't be an issue. If you do have errors, obviously you need to correct them now.)

8. Open your favorite IDE, open your project, and start developing.

9. When done, remove the plugin from your project and add it back again without linking (to verify that things work as expected):

cordova plugin rm your-plugin
cordova plugin add /path/to/your/plugin
(Test your app again)

10. When ready to publish to npm (if you intend to), you need a package.json:

plugman createpackagejson /path/to/your/plugin

11. Publish

npm adduser # (if you haven't already got an account)
npm publish /path/to/your/plugin

You should ideally add tests to your plugin as well, but I'll leave that to you to discover. You should be able to get started with the above, though.

On Monday, Februaory 15, 2016 at 10:43:48 AM UTC-6, Andrea Castellini wrote:
> Hi all
Reply all
Reply to author
Forward
0 new messages