使用plugman cordova 自定義插件 開發(fā) 測試 使用

1、準備

1.環(huán)境搭建 (ios)? andriod(http://www.wenzhixin.net.cn/2014/03/20/cordova_my_plugin)

cordova插件開發(fā)前需要安裝一些軟件和配置環(huán)境

1.1 node.js環(huán)境搭建

到node.js官網(wǎng)(https://nodejs.org/)下載安裝就好 熬拒, 或者命令行 ?用homebrew 也很方便愕宋;百度一堆資料

1.2 cordova 的安裝

在窗口輸入下面命令全局安裝cordova

npm install -g cordova

百度一堆資料

2.創(chuàng)建第一個應(yīng)用

創(chuàng)建的命令是cordova create

列如:

cordova create hello com.cool.hello HelloWorld

第一個參數(shù)hello表示在工程目錄中創(chuàng)建一個 hello 的文件夾

第二個參數(shù)com.cool.hello表示包名(反向域名)凌简,用于標志不同的 app

第三個參數(shù)HelloWorld表示項目的名稱,可以在 config.xml 文件中修改

3.添加平臺

3.1 進入創(chuàng)建的項目目錄

cd hello

3.2?查看已有的平臺

cordova platforms list

3.3添加所需要的平臺

cordova platform add ios

如果想移除已經(jīng)添加的平臺的話?cordova platform remove ios 或者cordova platform rm ios

4.編譯項目

編譯項目命令

cordova build ios


2歼争、開發(fā)

.插件開發(fā)

前面說了這么多全都是準備工作蹋笼,接下來是插件的具體開發(fā)過程

6.1 pluman的安裝

npm install -g plugman

如果permission denied ?(try: ?sudo npm install -g plugman)

6.2 plugman安裝完之后就可以創(chuàng)建一個插件了cordova plugin

plugman create --name --plugin_id --plugin_version [--path ] [--variableNAME=VALUE]

參數(shù):

pluginName: 插件名字

pluginID: 插件id, egg?:?coolPlugin

oversion: 版本, egg : 0.0.1

directory:一個絕對或相對路徑的目錄,該目錄將創(chuàng)建插件項目

variable NAME=VALUE: 額外的描述蜕该,如作者信息和相關(guān)描述

egg :?plugman create --name CoolPlugin --plugin_id coolPlugin --plugin_version 0.0.1

生成的插件的目錄如下: (這里復(fù)制 andriod的例子 )

插件名

? ? ?----src

? ? ? ? ? ? ++++ios

? ? ? ? ? ? ? ? ? ? ? ? ? ? ?\\\.m

? ? ?-----www?

? ? ? ? ? ? ? ?++++js?

? ? ------plugin.xml

? ? ------packet.json



執(zhí)行創(chuàng)建插件的終端命令后 自己做的小例子如下:

ios 文件(在xcode下編輯src文件下的 文件):

/********* StorageToolPlugin.m Cordova Plugin Implementation *******/

#import

@interface StorageToolPlugin : CDVPlugin {

// Member variables go here.

}

- (void)getValueMethod:(CDVInvokedUrlCommand*)command;

- (void)setValueMethod:(CDVInvokedUrlCommand*)command;

@end

@implementation StorageToolPlugin

- (void)getValueMethod:(CDVInvokedUrlCommand*)command

{

NSLog(@"oc getValueMethod");

CDVPluginResult* pluginResult = nil;

NSString* echo = [command.arguments objectAtIndex:0];

NSLog(@"參數(shù):%@",echo);

if (echo != nil && [echo length] > 0) {

NSString * value = [[NSUserDefaults standardUserDefaults] objectForKey:echo];

NSLog(@"value:%@",value);

if (value) {

NSLog(@"value:%@ 傳遞到ts ",value);

pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:value];

}else{

pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"沒有對應(yīng)的值"];

}

} else {

pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"參數(shù)為空犁柜,取值失敗"];

}

[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];

}

- (void)setValueMethod:(CDVInvokedUrlCommand*)command

{

CDVPluginResult* pluginResult = nil;

NSDictionary * ParaterDic = [command.arguments objectAtIndex:0];

NSLog(@"dic:%@",ParaterDic);

if (![ParaterDic isKindOfClass:[NSNull class]] && ParaterDic ) {

NSArray * arrValues = [ParaterDic allValues];

NSString * key, * value;

for (int i=0; i

{

if (i==0) {

key = arrValues[0];

}

if (i==1) {

value = arrValues[1];

}

}

NSLog(@"開始保存 key :%@ value:%@",key,value);

[[NSUserDefaults standardUserDefaults] setValue:value forKey:key];

pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"保存成功"];

} else {

pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"參數(shù)為空? 保存失敗I咚稹A尬隆坛怪!"];

}

[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];

}

@end

js文件:在auto 淤齐、webstorm 或者 ?vsCode 下編輯www文件下的?

varexec= require('cordova/exec');

varMyStorageTool=function() {}

MyStorageTool.prototype.getValue=function(arg0, success, error) {

exec(success, error,"StorageToolPlugin","getValueMethod", [arg0]);

}

MyStorageTool.prototype.setValue=function(arg0, success, error) {

exec(function(meg) {

alert(meg);

}, error,"StorageToolPlugin","setValueMethod", [arg0]);

}

varStorageTool=newMyStorageTool();

module.exports=StorageTool;

xml:



官網(wǎng)的介紹:

name: 插件的名字

<js-modul> 下的<clobbers target="xxxx"> ?xxx是js平臺的類 ? 可以在ts文件里聲明調(diào)用 ?在使用里會介紹使用方法

platform 使用支持的使用平臺

<feature> 下的 <param name="xxx" value="***"> ?xxx代表平臺包 ?***代表xxx平臺下支持的類

官方解釋: http://cordova.axuer.com/docs/zh-cn/latest/guide/hybrid/plugins/index.html

The top-levelplugintag'sidattribute uses the same reverse-domain format to identify the plugin package as the apps to they're added. Thejs-moduletag specifies the path to the common JavaScript interface. Theplatformtag specifies a corresponding set of native code, for theiosplatform in this case. Theconfig-filetag encapsulates afeaturetag that is injected into the platform-specificconfig.xmlfile to make the platform aware of the additional code library. Theheader-fileandsource-filetags specify the path to the library's component files.

packet.json

{

"name":"storagetoolplugin",

"version":"1.0.0",

"description":"sengled storageTool ",

"main":"index.js",

"scripts": {

"test":"echo\"Error: no test specified\"&& exit 1"

},

"author":"",

"license":"ISC"

}

3、測試

待補充

4袜匿、使用?

問題:

在ts文件調(diào)用插件的js方法問題 ? 下面是html ts文件使用?






有時間我會繼續(xù)補充 ?希望可以幫助新手 ?如果是老手多擔待 ?謝謝 ?

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末更啄,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子居灯,更是在濱河造成了極大的恐慌祭务,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,692評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件怪嫌,死亡現(xiàn)場離奇詭異义锥,居然都是意外死亡,警方通過查閱死者的電腦和手機岩灭,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,482評論 3 392
  • 文/潘曉璐 我一進店門拌倍,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事柱恤∈酰” “怎么了?”我有些...
    開封第一講書人閱讀 162,995評論 0 353
  • 文/不壞的土叔 我叫張陵梗顺,是天一觀的道長泡孩。 經(jīng)常有香客問我,道長寺谤,這世上最難降的妖魔是什么仑鸥? 我笑而不...
    開封第一講書人閱讀 58,223評論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮矗漾,結(jié)果婚禮上锈候,老公的妹妹穿的比我還像新娘。我一直安慰自己敞贡,他們只是感情好泵琳,可當我...
    茶點故事閱讀 67,245評論 6 388
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著誊役,像睡著了一般获列。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上蛔垢,一...
    開封第一講書人閱讀 51,208評論 1 299
  • 那天击孩,我揣著相機與錄音,去河邊找鬼鹏漆。 笑死巩梢,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的艺玲。 我是一名探鬼主播括蝠,決...
    沈念sama閱讀 40,091評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼饭聚!你這毒婦竟也來了忌警?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,929評論 0 274
  • 序言:老撾萬榮一對情侶失蹤秒梳,失蹤者是張志新(化名)和其女友劉穎法绵,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體酪碘,經(jīng)...
    沈念sama閱讀 45,346評論 1 311
  • 正文 獨居荒郊野嶺守林人離奇死亡朋譬,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,570評論 2 333
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了兴垦。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片徙赢。...
    茶點故事閱讀 39,739評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡庭呜,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出犀忱,到底是詐尸還是另有隱情募谎,我是刑警寧澤,帶...
    沈念sama閱讀 35,437評論 5 344
  • 正文 年R本政府宣布阴汇,位于F島的核電站数冬,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏搀庶。R本人自食惡果不足惜拐纱,卻給世界環(huán)境...
    茶點故事閱讀 41,037評論 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望哥倔。 院中可真熱鬧秸架,春花似錦、人聲如沸咆蒿。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,677評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽沃测。三九已至缭黔,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間蒂破,已是汗流浹背馏谨。 一陣腳步聲響...
    開封第一講書人閱讀 32,833評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留附迷,地道東北人惧互。 一個月前我還...
    沈念sama閱讀 47,760評論 2 369
  • 正文 我出身青樓,卻偏偏與公主長得像喇伯,于是被迫代替她去往敵國和親喊儡。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,647評論 2 354

推薦閱讀更多精彩內(nèi)容