Sunday, August 23, 2015

Building a Custom Cordova plugin for iOS


When looked for some sample to do this, found the article given in the reference section. In this example, when a text is sent from the browser, it is written to native file system using the FileWriter iOS class. 

First step is to create a cordova project. the command is as below. 

$ cordova create . CustomPlugin CustomPlugin
Creating a new cordova project.

Then added the iOS platform using the below command 

$ sudo cordova platform add ios 

Then ran the project and could see the cordova splash screen. 

To develop a custom plugin, added the feature tag to the config.xml file like below right under the tag 

name="FileWriter">
        name="ios-package" value="FileWriter" />
   


Below was the header definition of FileWriter header file. 

#import

@interface FileWriter : CDVPlugin

// This will return the file contents in a JSON object via the getFileContents utility method
- (void) cordovaGetFileContents:(CDVInvokedUrlCommand *)command;

// This will accept a String and call setFileContents to persist the String on to disk
- (void) cordovaSetFileContents:(CDVInvokedUrlCommand *)command;

#pragma mark - Util_Methods

// Pure native code to persist data
- (void) setFileContents;

// Native code to load data from disk and return the String.
- (NSString *) getFileContents;

@end



References:

No comments:

Post a Comment