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! "); }
|