Описание тега phonegap-plugins
A Cordova (formerly PhoneGap, see phonegap) plugin bridges a bit of functionality between the WebView powering a Cordova application and the native platform the Cordova application is running on. Plugins are composed of a single JavaScript interface used across all platforms, and native implementations following platform-specific plugin interfaces that the JavaScript will call into.
JavaScript
The entry point for any plugin is JavaScript. The reason developers use Cordova is so they can use and write JavaScript, not Objective-C, not Java, not C#. The JavaScript interface for your plugin is the front-facing and arguably most important part of your Cordova plugin.
You can structure your plugin’s JavaScript however you like. The one thing you must use to communicate between the Cordova JavaScript and native environments is the cordova.exec
function. Here is an example:
cordova.exec(function(winParam) {}, function(error) {}, "service",
"action", ["firstArgument", "secondArgument", 42, false]);
The parameters explained in more detail as follows:
function(winParam) {}
- Success function callback. Assuming your exec call completes successfully, this function will be invoked (optionally with any parameters you pass back to it)function(error) {}
- Error function callback. If the operation does not complete successfully, this function will be invoked (optionally with an error parameter)"service"
- The service name to call into on the native side. This will be mapped to a native class. More on this in the native guides below"action"
- The action name to call into. This is picked up by the native class receiving the exec call, and, depending on the platform, essentially maps to a class's method. For more detail please check out the native guides located at the end of this article.[/* arguments */]
- Arguments to get passed into the native environment
Native
Once you have defined a JavaScript for your plugin, you need to complement it with at least one native implementation.
For example, in ios, this native implementation would include doing the following:
- Creating a
.h
and.m
class for the plugin - Registering the class name in the
Cordova.plist
file - Creating the function we called in the javascript as one of the instance methods of that class
- Handling our callback situations (if javascript expected a callback).
Further Reading
Links to learn more about the various platforms follow: