使用plugman cordova 自定义插件 开发 测试 使用

1、准备

1.环境搭建 (ios)  andriod(http://www.wenzhixin.net.cn/2014/03/20/cordova_my_plugin)

cordova插件开发前需要安装一些软件和配置环境

1.1 node.js环境搭建

到node.js官网(https://nodejs.org/)下载安装就好 , 或者命令行  用homebrew 也很方便;百度一堆资料

1.2 cordova 的安装

在窗口输入下面命令全局安装cordova

npm install -g cordova

百度一堆资料

2.创建第一个应用

创建的命令是cordova create

列如:

cordova create hello com.cool.hello HelloWorld

第一个参数hello表示在工程目录中创建一个 hello 的文件夹

第二个参数com.cool.hello表示包名(反向域名),用于标志不同的 app

第三个参数HelloWorld表示项目的名称,可以在 config.xml 文件中修改

3.添加平台

3.1 进入创建的项目目录

cd hello

3.2 查看已有的平台

cordova platforms list

3.3添加所需要的平台

cordova platform add ios

如果想移除已经添加的平台的话 cordova platform remove ios 或者cordova platform rm ios

4.编译项目

编译项目命令

cordova build ios


2、开发

.插件开发

前面说了这么多全都是准备工作,接下来是插件的具体开发过程

6.1 pluman的安装

npm install -g plugman

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

6.2 plugman安装完之后就可以创建一个插件了cordova plugin

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

参数:

pluginName: 插件名字

pluginID: 插件id, egg : coolPlugin

oversion: 版本, egg : 0.0.1

directory:一个绝对或相对路径的目录,该目录将创建插件项目

variable NAME=VALUE: 额外的描述,如作者信息和相关描述

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

生成的插件的目录如下: (这里复制 andriod的例子 )

插件名

     ----src

            ++++ios

                             \\\.m

     -----www 

               ++++js 

    ------plugin.xml

    ------packet.json



执行创建插件的终端命令后 自己做的小例子如下:

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(@"参数:%@",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:@"没有对应的值"];

}

} else {

pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"参数为空,取值失败"];

}

[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:@"参数为空  保存失败!!!"];

}

[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:



官网的介绍:

name: 插件的名字

<js-modul> 下的<clobbers target="xxxx">  xxx是js平台的类   可以在ts文件里声明调用  在使用里会介绍使用方法

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文件调用插件的js方法问题   下面是html ts文件使用 






有时间我会继续补充  希望可以帮助新手  如果是老手多担待  谢谢  

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,560评论 4 361
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,104评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,297评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,869评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,275评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,563评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,833评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,543评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,245评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,512评论 2 244
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,011评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,359评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,006评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,062评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,825评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,590评论 2 273
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,501评论 2 268

推荐阅读更多精彩内容