Cordova 3.0 + iOS Custom plugins

Source: Internet
Author: User
Tags file copy

<pre name= "code" class= "OBJC" ><span style= "FONT-SIZE:14PX;" > For half a month Cordova plug-in, but has not sunk heart can get. In order to better understand the development of Cordova plug-in, today I wrote a demo first brief introduction, if not clear Cordova is what, what role, can move to ask Niang "hybrid application development, Cordova framework". In fact, Cordova's implementation principle is based on the WebView intercept URL (the URL contains the class name and the method name and parameters), and then through the reflection mechanism to implement the native code calls.  OK, let's start by enumerating the main steps and ideas of custom plugin.  1, installation Cordova environment.  2. Create a project using Cordova.  3. Open iOS project.  Core Step 4, write OC native code plug-in class, need to inherit from Cdvplugin parent class.  5, write the JS layer code called OC. 6, write the top-level plugin.xml plug-in configuration file. </span><span style= "FONT-SIZE:18PX;" > Dry Goods Come, detailed steps explained. </span><span style= "FONT-SIZE:14PX;"    > 1, installation Cordova environment.  Here on Baidu Cordova, the officer of the Internet cafes, the steps are not elaborate, relatively simple.    2. Create a project using Cordova.    Crodova Create is a command, followed by three parameters.    Cordova Create project name (Testplugin) app logo (com.liki.TestPlugin) app name plugin 3, open iOS project.  The project path is in the platforms folder that is currently created under the project path.   4, write the OC native code plug-in class, you need to inherit from the Cdvplugin parent Class 5, the iOS and Android native class files copy the corresponding directory. 6, the written JS file copy to the WWW directory </span><span style= "FONT-SIZE:18PX;" ></span><pre name= "code" class= "OBJC" ><span style= "FONT-SIZE:14PX;" >ceshiplugin.h</span><spaN style= "font-family:arial, Helvetica, Sans-serif;" > Files </span>
#import <Cordova/CDV.h> @interface ceshiplugin:cdvplugin-(void) Addstr: (Cdvinvokedurlcommand *) command;@ End

<span style= "FONT-SIZE:14PX;" > ************** I was delimiter ***********************************</span><span style= "font-size:18px;" ></span>
<span style= "FONT-SIZE:18PX;" ></span><pre name= "code" class= "OBJC" ><span style= "FONT-SIZE:14PX;" >ceshiplugin.m</span><span style= "font-family:arial, Helvetica, Sans-serif;" > Files </span>
#import "CeshiPlugin.h" @implementation ceshiplugin-(void) Addstr: (Cdvinvokedurlcommand *) command{//Get the value JS passed over Cdvpluginresult *result=nil; NSString *ceshi1=[command.arguments objectatindex:0]; NSString *ceshi2=[command.arguments Objectatindex:1]; NSString *ceshi3=[command.arguments Objectatindex:2]; if (Ceshi1!=nil&&[ceshi1 length]>0&&[ceshi2 length]>0&&ceshi2!=nil&&ceshi3! =nil&&[ceshi3 length]>0) {//splicing string nsstring *addresult=[nsstring stringwithformat:@ "%@%@%@", Ceshi1,ceshi2 , Ceshi3]; Result=[cdvpluginresult RESULTWITHSTATUS:CDVCOMMANDSTATUS_OK Messageasstring:addresult]; Transmit value (message) to JS file//[Self.commanddelegate Sendpluginresult:result callbackId:command.callbackId]; }else {result = [Cdvpluginresult resultwithstatus:cdvcommandstatus_error messageasstring:@ "Cuowu"];//[ Self.commanddelegate Sendpluginresult:result CallbackId:command.callbackId]; } [Self.commanddelegate Sendpluginresult:result callbackId:command.callbackId];} @end
<span style= "FONT-SIZE:14PX;" > ************** I'm the delimiter ***********************************</span>
<span style= "FONT-SIZE:18PX;" ></span><pre name= "code" class= "OBJC" ><span style= "FONT-SIZE:14PX;" >ceshiplugin.js</span><span style= "font-family:arial, Helvetica, Sans-serif;" > Files </span>
cordova.define ("Com.example.CeshiPlugin.CeshiPlugin", function (require, exports, module) {var wang=require (' Cordova/exec '); module.exports={//Custom method Addstr:function (Addsuc,addfaild,args, ARGS2, ARGS3) {/* first parameter: Callback success parameter second parameter: callback failure parameter The third parameter: JS to call the OC Class name parameter fourth parameter: to invoke the name of the method fifth parameter: The parameter to be passed in JSON format */Wang (addsuc,addfaild, "Ceshiplugin", "addstr", [ARGS,ARGS2,ARGS3 ]); } };});
<span style= "FONT-SIZE:14PX;" > ************** I'm the delimiter ***********************************</span>
<span style= "FONT-SIZE:14PX; Font-family:arial, Helvetica, Sans-serif; ></span>
<span style= "font-family:arial, Helvetica, Sans-serif;" ><span style= "FONT-SIZE:18PX;" >plugin.xml file </span></span>
<span style= "FONT-SIZE:14PX;" ><?xml version= "1.0" encoding= "UTF-8"?><!--id= Plug-in unique identity--><plugin xmlns= "http://apache.org/ cordova/ns/plugins/1.0 "id=" com.example.CeshiPlugin "//This ID should be consistent with the identity of the second parameter when the project was created version=" 2.0 "><!--name: Plug Item Name-<name>HelloPlugin</name><!--Description: Description Information-<description> This plugin allows    You to show a myplugin.  A Toast is a little non intrusive buttonless popup which automatically disappears. </description> <license>MIT</license><!--js-module: Corresponding to our JavaScript file, the SRC attribute points to the www/xxx.js path-- > <js-module src= "www/ceshiplugin.js" name= "Ceshiplugin" > <clobbers target= "myplugin"/> </js-modul E> <!--platform: Supported platforms iOS--<platform name= "ios" > <config-file target= "config" parent= "/*"      ; <feature name= "Ceshiplugin" > <param name= "ios-package" value= "Ceshiplugin"/> </feature> & lt;/config-file&Gt 
<span style= "FONT-SIZE:14PX;"   > ************** I am the delimiter *********************************** 7, to this basic OK, you can pack the plug-ins, self-test and submitted to others to use. Packaging steps: 1, the establishment of a plug-in root directory, the best format is as follows: Com.liki.</span><span style= "FONT-SIZE:14PX; Font-family:arial, Helvetica, Sans-serif; >xxxplugin</span><span style= "FONT-SIZE:14PX;"   > 2, set up the WWW and src directories under Xxxplugin.   3. Copy the Plugin.xml configuration file to the Xxxplugin directory.   4, in the SRC directory to build the iOS directory, other platforms are OK, the author did not try, but the official network said support.   5. Copy the original iOS class file into the corresponding directory. 6, copy the written JS file to the WWW directory. </span>
<span style= "FONT-SIZE:14PX;" ></span><pre name= "code" class= "OBJC" ><span style= "FONT-SIZE:14PX;" > ************** I'm the delimiter ***********************************</span>
Using the plug-in steps, here is a brief explanation, because the need to have Cordova and JS and HTML-related knowledge, can only give the demo code
<span style= "FONT-SIZE:14PX;" ></span><span style= "FONT-SIZE:18PX;" >html file calls </span><span style= "FONT-SIZE:14PX;" >  <script type= "Text/javascript" >            document.addeventlistener ("Deviceready", Ondeviceready, False) ;            function Ondeviceready () {                /**                 * loaded successfully called JS                /myplugin.addstr (Addsuc,addfiald, "Wang", "Zhao", "bin") ;                            }        </script>                <script√>            /**             * JS callback function */        function addsuc (result) {            alert (' Callback succeeded Addsuc= ' +result);        }        function Addfiald (Result) {            alert (' addfiald= ' +result);        }        </script></span>




Cordova 3.0 + iOS Custom plugins

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.