iOS - Implementing Apache Cordova plugin
In some cases, developers of a MobileFirst application might have to use a specific third-party native library or a device function that is not yet available in Apache Cordova.
This tutorial demonstrates how to create and integrate a simple Apache Cordova plug-in for iOS, in the following topics:
- Creating a plug-in
- Declaring a plug-in
- Implementing the Objective-C code of a Cordova plug-in
- Sample application
Note: In Cordova-based applications, developers must check for the
deviceready event before they use the Cordova API set. In a MobileFirst application, however, this check is done internally.
Instead of implementing this check, you can place implementation code in the
wlCommonInit() function in the
Creating a plug-in
- Declare the plug-in in the
- Use the
- Create the plug-in class that will run natively in iOS.
Declaring a plug-in
You must declare the plug-in in the project, so that Cordova can detect it.
To declare the plug-in, add a reference to the
config.xml file, located in the native folder of the iOS environment.
cordova.exec() method to call the Cordova plug-in:
sayHelloSuccess - Success callback
sayHelloFailure - Failure callback
SayHelloPlugin - Plug-in name as declared in the
sayHello - Action name
[name] - Parameters array
The plug-in calls the
Implementing the Objective-C code of a Cordova plug-in
Prequisite: Ensure that the project is built in Eclipse and opened in the Xcode IDE.
- Add a new Cocoa Touch Class file, make sure that it is a subclass of
UIViewController, and save it in the
Classesfolder of the Xcode project.
- Import the
Cordova/CDV.hheader file and inherit the
- Declare the
- Implement the method. The
CDVCommandStatusparameter defines whether the plug-in call was successful or not.
- Use the
If you work with existing
.h files, reference them while you are working in Xcode.
.h files only in the
iphone\native\Classes folder in Eclipse is not sufficient, because these files are not referenced in the Xcode project unless they were added in Xcode.
Click to download the Studio project.