Implementation of iOS plug-in based on PhoneGap3.4 framework

Source: Internet
Author: User

Phonegap provides basic feature interfaces for iOS devices to call HTML pages. However, these basic interfaces cannot meet some of our special requirements, so sometimes we need to develop plug-ins to expand their functions. IOS plug-in development based on the PhoneGap3.4 framework involves the following steps:

1) set up the iOS development environment of PhoneGap3.4. Refer to this article for the setup steps.

2) Compile the. h header file. The sample code is as follows:

# Import

@ Interface CDVLogin: CDVPlugin
-(Void) login :( CDVInvokedUrlCommand *) command;
@ End

3) Compile the. m source code file. The sample code is as follows:

# Import "CDVLogin. h"

@ Implementation CDVLogin
-(Void) login :( CDVInvokedUrlCommand *) command {
NSString * echo = @ "NIL ";
// Plug-in return value
CDVPluginResult * pluginResult = nil;
// Obtain the passed parameters
NSString * param = [command. arguments objectAtIndex: 0];
Boolean loginStatus = [self loginSystem: param];
If (loginStatus ){
Echo = @ "YES ";
// Return value of the plug-in upon successful completion
PluginResult = [CDVPluginResult resultWithStatus: CDVCommandStatus_ OK messageAsString: echo];
} Else {
Echo = @ "NO ";
// Return value of the plug-in upon failure
PluginResult = [CDVPluginResult resultWithStatus: CDVCommandStatus_ERROR messageAsString: echo];
}
[Self. commandDelegate sendPluginResult: pluginResult callbackId: command. callbackId];

}
-(Boolean) loginSystem :( NSString *) para
{
Return YES;
}
@ End

4) register the plug-in config. xml as follows:



5) js call:

Var CustomPlugin = {
CallNativeMethod: function (success, fail, param ){
Var exec = cordova. require ("cordova/exec ");
Return exec (success, fail, "Login", "login", [param]);
}
};

Function callNativePlugin (param ){
CustomPlugin. callNativeMethod (nativePluginResultHandler, nativePluginErrorHandler, param );
}
Function nativePluginResultHandler (result ){
// Alert ("SUCCESS: \ r \ n" + result );
}
Function nativePluginErrorHandler (error ){
If (error = "NO "){
Alert ("Call failed! ");
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.