標籤:cordova ios開發 外掛程式
<pre name="code" class="objc"><span style="font-size:14px;">搞了半個月Cordova外掛程式,但一直沒沉下心能弄。為了更好的理解Cordova外掛程式的開發,今天自己寫了一個Demo 先簡單介紹一下,如不清楚Cordova是什麼,有什麼作用,可以移步問度娘《hybrid應用開發,Cordova架構》。其實Cordova的實現原理還是基於WebView截獲URL(URL中包含類名和方法名以及參數),然後通過反射機制實現原生代碼調用。 好,先列舉一下Custom Plugin的主要步驟和思路。 1、安裝Cordova環境。 2、使用Cordova建立工程。 3、開啟IOS工程。 //核心步驟 4、編寫OC原生代碼外掛程式類,需要繼承自CDVPlugin父類。 5、編寫調用OC的JS層代碼。 6、編寫頂級的Plugin.xml的外掛程式設定檔。</span><span style="font-size:18px;">乾貨來了,詳細步驟說明。</span><span style="font-size:14px;"> 1、安裝Cordova環境。 這裡就百度Cordova,上官網吧,步驟繁瑣就不細說,比較簡單。 2、使用Cordova建立工程。 Crodova create 是命令,後面跟三參數。 cordova create 工程名(TestPlugin) 應用標識(com.liki.TestPlugin) App名稱Plugin 3、開啟IOS工程。 工程路徑在:當前建立工程路徑 下的 platforms檔案夾中。 4、編寫OC原生代碼外掛程式類,需要繼承自CDVPlugin父類5、將ios和Android原生的類檔案拷貝相應目錄。 6、將寫好的JS檔案拷貝到www目錄下</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;">檔案</span>
#import <Cordova/CDV.h>@interface CeshiPlugin : CDVPlugin-(void)addstr:(CDVInvokedUrlCommand *)command;@end
<span style="font-size:14px;"> **************我是分隔字元***********************************</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;">檔案</span>
#import "CeshiPlugin.h"@implementation CeshiPlugin-(void)addstr:(CDVInvokedUrlCommand *)command{ //擷取js傳過來的值 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) { //拼接字串 NSString *addResult=[NSString stringWithFormat:@"%@%@%@",ceshi1,ceshi2,ceshi3]; result=[CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:addResult]; //傳值(訊息)到JS檔案// [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;"> **************我是分隔字元***********************************</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;">檔案</span>
cordova.define("com.example.CeshiPlugin.CeshiPlugin", function(require, exports, module) { var wang=require(‘cordova/exec‘); module.exports={ //自訂方法 addstr:function(addSuc,addFaild,args, args2, args3) { /* 第一個參數:回調成功參數 第二個參數:回調失敗參數 第三個參數:JS要調用OC類的名字參數 第四個參數:要調用方法的名字 第五個參數:要傳遞的參數,以JSON格式 */ wang(addSuc,addFaild,"CeshiPlugin","addstr",[args,args2,args3]); } };});
<span style="font-size:14px;"> **************我是分隔字元***********************************</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檔案</span></span>
<span style="font-size:14px;"><?xml version="1.0" encoding="UTF-8"?><!--id=外掛程式的唯一標識--><plugin xmlns="http://apache.org/cordova/ns/plugins/1.0" id="com.example.CeshiPlugin"//此ID需與建立項目時的第二個參數即應用標識一致 version="2.0"><!--name:外掛程式的名稱--> <name>HelloPlugin</name><!--description:描述資訊--> <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:對應我們的 javascript 檔案,src 屬性指向 www/xxx.js路徑--> <js-module src="www/CeshiPlugin.js" name="CeshiPlugin"> <clobbers target="myplugin" /> </js-module> <!-- platform:支援的平台ios --> <platform name="ios"> <config-file target="config.xml" parent="/*"> <feature name="CeshiPlugin"> <param name="ios-package" value="CeshiPlugin"/> </feature> </config-file> <header-file src="src/ios/CeshiPlugin.h"/> <source-file src="src/ios/CeshiPlugin.m"/> <framework src="QuartzCore.framework" /> </platform></plugin></span>
<span style="font-size:14px;"> **************我是分隔字元*********************************** 7、到這就基本OK了,可以將外掛程式打包整理好,自己測試和提交給別人使用了。 打包步驟: 1、建立一個外掛程式根目錄,格式最好如下:com.liki.</span><span style="font-size: 14px; font-family: Arial, Helvetica, sans-serif;">xxxPlugin</span><span style="font-size:14px;"> 2、在xxxPlugin下建立www和src目錄。 3、將plugin.xml設定檔拷貝到xxxplugin目錄。 4、在src目錄下建立ios目錄,其他平台也行,筆者沒有試過,不過官網上說支援。 5、將ios原生的類檔案拷貝相應目錄內。 6、將寫好的JS檔案拷貝到www目錄下。</span>
<span style="font-size:14px;"></span><pre name="code" class="objc"><span style="font-size:14px;"> **************我是分隔字元***********************************</span>
使用外掛程式步驟,這裡就簡單說明一下,因為需要具備Cordova和JS以及Html相關知識,只能給示範代碼
<span style="font-size:14px;"></span><span style="font-size:18px;">Html檔案調用</span><span style="font-size:14px;"> <script type="text/javascript"> document.addEventListener("deviceready", onDeviceReady, false); function onDeviceReady() { /** * 載入成功調用js */ myplugin.addstr(addSuc,addFiald,"wang","zhao","bin"); } </script> <script √> /** * js回呼函數 */ function addSuc(result) { alert('回調成功addSuc='+result); } function addFiald(result) { alert('addFiald='+result); } </script></span>
cordova 3.0以上ios自訂外掛程式