Third, how to develop Cordova plugin @ IOS with XCode

Source: Internet
Author: User

Note: This article is original article, welcome reprint, but please specify Source: http://www.cnblogs.com/xdxer/p/4127220.html

One: What is the Cordova plugin

Plugin Development guide:http://cordova.apache.org/docs/en/edge/guide_hybrid_plugins_index.md.html#plugin% 20development%20guide

Cordova's official plugin contains a lot of functionality to invoke hardware devices, file systems, and network requests. So the Cordova plug-in provides functionality that can be summed up by implementing certain functions with the native code and providing JavaScript with an interface that can be easily invoked.

(Plug-in mode of operation for me to be free I will do analysis, mainly on the CDV of the series of source code analysis)

Two: some small details

# iOS plugins are implemented by classes that inherit from the Cdvplugin class.

#在JavaScript方面提供了统一的调用方法:

Cordova.exec (<SUCCESSFUNCTION>,//correct return then run the JS function

<failfunction>,////To run the method if it fails to return

<serviceclass>,//Plugin OBJC class

<actionmethod>,//The name of the handler function in the plug-in class

[<args>]//Parameters passed to the plug-in

);

From the above structure we can make a little speculation and analysis: first of all these things will be loaded in the request, is handled by a function of processing requests in the uiwebviewdelegate, and the plug-in class and method names are passed through the way of strings, So you can probably guess that Cordova in the bottom-level implementation of some of the methods used in Objc_runtime, such as objc_sendmsg, in fact, as long as the reference OBJC in the class is how to express the structure in C language, You can understand OBJC as a natural advantage of a language that passes messages to an object for function calls (of course, the cost of the call is very high, and most of the time it can be optimized efficiently using the Imp caching method). Again to see the parameters passed, JS can pass the parameters are actually very simple, then a few, most of them are strings, or an object, and the object may need to be standardized by JSON, and occasionally may face the problem of character encoding (Unicode Viva ~).

The <param name = "onload" value = "true" is set in the #在config. XML plug-in property/> You can initialize the plug-in object when WebView load

#有的时候一个插件可能会运行很长时间 (long-running request | | Background activity) requires the implementation of the OnReset method, which is called when the JavaScript is reload (to be researched).

#可以使用block, call Runinbackground in the plug-in class.

Three: Demo

First add such a statement to CONFIG. sql:

<name= "plugintest">        <name  = "Ios-package"= "aspplugintest"/></feature >

Add the following plug-in classes:

Code:

ASPPluginTest.h

// //   ASPPluginTest.h//  Testcordova////  Created by Xia Dongxiang on 14-11-27. //   Copyright (c) 2014 Xia Dongxiang. All rights reserved. // #import <Cordova/CDVPlugin.h>@interface  aspplugintest:cdvplugin{    }-( void) Test: (Cdvinvokedurlcommand *) command; @end

Aspplugintest.m

////ASPPLUGINTEST.M//Testcordova////Created by Xia Dongxiang on 14-11-27.//Copyright (c) 2014 Xia Dongxiang. All rights reserved.//#import "ASPPluginTest.h"@implementationaspplugintest-(void) Test: (Cdvinvokedurlcommand *) command{Cdvpluginresult* Pluginresult =Nil; NSString*string= [Command.arguments Objectatindex:0]; if([stringIsequaltostring:@"Test"]) {Pluginresult= [Cdvpluginresult resultwithstatus:cdvcommandstatus_ok messageasstring:@"Test successful!"]; }    ElsePluginresult= [Cdvpluginresult resultwithstatus:cdvcommandstatus_error messageasstring:@"ERROR string!"]; [Self.commanddelegate Sendpluginresult:pluginresult callbackId:command.callbackId];}@end

In terms of JS, we can call this:

<Script>            varTestplugin={success:function(String) {Console.log ('JavaScript Success Callback:'+string); }, fail:function(String) {Console.log ('JavaScript fail callback:'+string); }, Test:function() {cordova.exec (Testplugin.success,testplugin.fail,"plugintest","Test",['Test']);                    }                            }; </Script>        <BR><BR><BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;& nbsp;&nbsp;        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;& nbsp;&nbsp;&nbsp;&nbsp;&nbsp;                <Buttontype= "button"onclick= "testplugin.test ();" >Test My Plugin</Button>

In this case, we encapsulated the Exec method and loaded both success and fail into an object for ease of invocation and prevention of renaming.

Effect

Call succeeded!

If there is a hint that the call speed is relatively slow to use 18ms, then we can use the above mentioned method to optimize, using block + commanddelegate Runinbacnground method to solve the problem!

Third, how to develop Cordova plugin @ IOS with XCode

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.