1、準(zhǔn)備
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)建第一個(gè)應(yīng)用
創(chuàng)建的命令是cordova create
列如:
cordova create hello com.cool.hello HelloWorld
第一個(gè)參數(shù)hello表示在工程目錄中創(chuàng)建一個(gè) hello 的文件夾
第二個(gè)參數(shù)com.cool.hello表示包名(反向域名),用于標(biāo)志不同的 app
第三個(gè)參數(shù)HelloWorld表示項(xiàng)目的名稱,可以在 config.xml 文件中修改
3.添加平臺(tái)
3.1 進(jìn)入創(chuàng)建的項(xiàng)目目錄
cd hello
3.2?查看已有的平臺(tái)
cordova platforms list
3.3添加所需要的平臺(tái)
cordova platform add ios
如果想移除已經(jīng)添加的平臺(tái)的話?cordova platform remove ios 或者cordova platform rm ios
4.編譯項(xiàng)目
編譯項(xiàng)目命令
cordova build ios
2、開發(fā)
.插件開發(fā)
前面說(shuō)了這么多全都是準(zhǔn)備工作,接下來(lái)是插件的具體開發(fā)過(guò)程
6.1 pluman的安裝
npm install -g plugman
如果permission denied ?(try: ?sudo npm install -g plugman)
6.2 plugman安裝完之后就可以創(chuàng)建一個(gè)插件了cordova plugin
plugman create --name --plugin_id --plugin_version [--path ] [--variableNAME=VALUE]
參數(shù):
pluginName: 插件名字
pluginID: 插件id, egg?:?coolPlugin
oversion: 版本, egg : 0.0.1
directory:一個(gè)絕對(duì)或相對(duì)路徑的目錄,該目錄將創(chuàng)建插件項(xià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:@"沒(méi)有對(duì)應(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ù)為空? 保存失敗?。?!"];
}
[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平臺(tái)的類 ? 可以在ts文件里聲明調(diào)用 ?在使用里會(huì)介紹使用方法
platform 使用支持的使用平臺(tái)
<feature> 下的 <param name="xxx" value="***"> ?xxx代表平臺(tái)包 ?***代表xxx平臺(tái)下支持的類
官方解釋: 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、測(cè)試
待補(bǔ)充
4、使用?
問(wèn)題:
在ts文件調(diào)用插件的js方法問(wèn)題 ? 下面是html ts文件使用?

