Friday, July 31, 2015

iOS Coredova Plugin - An Overview

The call exec(, , , ,[args]); from javascript code effectively marshals the request from UIWebView to the native iOS side calling the method on the service class passing the arguments to the action method. 

The plugin needs to be defined as a Feature tag in the XML file. The plugin xml file needs to specify the tags and these tags gets injected into the config.xml file. 

Plugins can utilize the pluginInitialize method to start up the services. Plugins should also implement the onReset method to stop the services if there are long running tasks. This method get called whenever the WebView changes the focus of the page. 

The general format of the cordova plugin method is like below 

-(void) pluginMethodSample:(CDVInvokeUrlCommand*)command
{
CDVPluginResult *pluginResult = nil;
NSString argument1 = [command.arguments objectAtIndex:0];
if(argument1 == nil)
{
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@“command was nil”];
}
else 
{
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
}
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}

For the return type, we can have String, Int, Double, Bool, Array, Dictionary, ArrayBuffer and multipart types. 

The complex return types include, 

messageAsArrayBuffer expects as NSData and converts as ArrayBuffer in the javascript callback. Likewise, ArrayBuffer sent from javascript is converted into NSData in iOS argument 
messageAsMultiPart expects and NSArray containing any of the other supported types, and sends the entire array as the arguments to the Javascript callback. This way, all the arguments are 
serialized and deserialized as necessary. It is safe to result back NSData as as mutlipart but not as Array/Dictionary. 

References:

No comments:

Post a Comment