In Cordova, a native function that can be called from JavaScript is made using a plugin. Take a look at the Plugin Development Guide to create your own plugin or search for already existing plugins that implement the functionality that you need.
This sample is extracted from the iOS Plugin Development Guide:
1. Declare javascript function
window.echo = function(str, callback) {
cordova.exec(callback, function(err) {
callback('Nothing to echo.');
}, "Echo", "echo", [str]);
};
2. Implement platform specific code:
/********* Echo.h Cordova Plugin Header *******/
#import
@interface Echo : CDVPlugin
- (void)echo:(CDVInvokedUrlCommand*)command;
@end
/********* Echo.m Cordova Plugin Implementation *******/
#import "Echo.h"
#import
@implementation Echo
- (void)echo:(CDVInvokedUrlCommand*)command
{
CDVPluginResult* pluginResult = nil;
NSString* echo = [command.arguments objectAtIndex:0];
if (echo != nil && [echo length] > 0) {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:echo];
} else {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR];
}
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
@end
3. Create plugin config file:
<platform name="ios">
<config-file target="config.xml" parent="/*">
<feature name="Echo">
<param name="ios-package" value="Echo" />
</feature>
</config-file>
</platform>
4. Call this function from Javascript:
window.echo("echome", function(echoValue) {
alert(echoValue == "echome"); // should alert true.
});