I'm working on a Phonegap/Cordova(version 2.9.0) custom plugin creation for iOS app. My steps are as follows :
I created a HelloPlugin.js file and copy it under www/js/ folder, its having code :
var HelloPlugin =
{
callNativeFunction: function (success, fail, resultType)
{
alert('a');
return Cordova.exec( success, fail, "HelloPlugin", "nativeFunction", ['1']);
}
};
I created HelloPlugin.h and HelloPlugin.m files under plugins folder, code :
// .h
#import <Cordova/CDVPlugin.h>
@interface HelloPlugin : CDVPlugin
- (void)nativeFunction:(CDVInvokedUrlCommand*)command;
@end
// .m
#import "HelloPlugin.h"
@implementation HelloPlugin
- (void)nativeFunction:(CDVInvokedUrlCommand*)command
{
NSLog(@"Hello, this is a native function called from PhoneGap/Cordova!");
}
@end
I added following code to config.xml file :
<feature name="HelloPlugin">
<param name="ios-package" value="CDVPlugin"/>
</feature>
At last I modified index.html in following way :
JS code added :
function callNativePlugin(returnSuccess)
{
HelloPlugin.callNativeFunction( nativePluginResultHandler, nativePluginErrorHandler, returnSuccess );
}
function nativePluginResultHandler (result) {
alert("SUCCESS: \r\n"+result );
}
function nativePluginErrorHandler (error) {
alert("ERROR: \r\n"+error );
}
Two buttons added and function called:
"callNativePlugin('success');" "callNativePlugin('error');"
I hope this is the only required things I need to do for activating plugin.
Issue : While running the app, I am getting FAILED pluginJSON error on console.
Output :
-[CDVCommandQueue executePending] [Line 116] FAILED pluginJSON = [ "HelloPlugin2650437", "HelloPlugin", "nativeFunction", [ "1", "1", "1" ] ]
What mistake I have done, please let me know. I really Appreciate your efforts. Please help me here.
First thing that jumped out is your plugin package name. It should be your iOS class name which is "HelloPlugin".
<param name="ios-package" value="HelloPlugin"/>
One of the purpose of the new way of referencing plugin is to allow flexibility and backward compatibility of plugin names especially on Android. Example:
<feature name="HelloPlugin">
<param name="ios-package" value="HelloCDVPlugin"/>
<param name="android-package" value="com.phonegap.plugins.HelloCDVPlugin"/>
</feature>
Where "HelloCDVPlugin" is your iOS class name and "com.phonegap.plugins.HelloCDVPlugin" is your Android class name.
Use below code for js
cordova.define("cordova/plugin/hello",
function (require, exports, module) {
var exec = require('cordova/exec');
function greet(name, win, fail) {
exec(win, fail, "Hello",
"greet", [name]);
}
module.exports = {
greet: greet
}
}
);
and change cordova.exec to
exec(this.callbacks.onSuccess, this.callbacks.onError, "Hello", "greet", [defaults]);
You can find example from below link
https://github.com/cristobal/phonegap-ios-datepicker-plugin
You have to modify the code as per your requirement.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With