1.配置环境
1. cordova是利用nodeJS进行管理的, 首先下载nodeJS (http://nodejs.org)
2. 使用命令行安装
sudo npm install -g cordova
3. 创建工程
cordova create hello(文件夹名) com.example.hello(id) HelloWorld(工程名)
4.添加相关组件并运行
cd hello
cordova platform add ios
cordova build
2.写插件
iOS类, 放入目录Plugins下
#import <Cordova/CDVPlugin.h>
@interface Test2 : CDVPlugin
- (void)test:(CDVInvokedUrlCommand*)command;
@end
#import "Test2.h"
@implementation Test2
-(void)test:(CDVInvokedUrlCommand *)command
{
CDVPluginResult * pluginResult = nil;
NSString* echo = [command.arguments objectAtIndex:0];
if (echo != nil && [echo length] > 0) {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"ios send OK"];
} else {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR];
}
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
@end
3.Login.html文件加入工程
路径: hello->platforms->ios->www下(工程中Staging下www)
4.login,js文件加入工程
路径: hello->platforms->ios->www->plugins->tansun-plugin-login->www下(路径不存在就新建)
5.文件配置
config.xml中添加
<feature name="Login">
<param name="ios-package" value="Test2" />
<param name="onload" value="true" />
</feature>
其中Login为自定义名字 Test2为插件类名
cordova_plugins.js中添加
{
"file": "plugins/tansun-plugin-login/www/login.js",//login.js路径
"id": "com.gldjc.guangcaiclient.login",//js文件定义的id
"clobbers": [
"Login"//config.xml中自定义的名字
]
}
6.使用
继承类
#import <Cordova/CDVViewController.h>
@interface MainViewController : CDVViewController
重写init方法
设置self.startPage = @"Login.html";
7.通讯
OC主动调JS
id <CDVWebViewEngineProtocol> engine = self.webViewEngine;
if(engine) {
[engine evaluateJavaScript:[NSString stringWithFormat:@"方法名('%@','%@')", self.name, self.password] completionHandler:^(id result, NSError *error) {
NSLog(@"result == %@, error == %@", error);
}];
}
JS通过cordova调OC
function mybtn(){
Login.loginSuccess(); //Login为自定义的名字
}
loginSuccess:function() {
exec(function(PluginResult){
alert(PluginResult);
}, null, "Login", "test", ["AAAAA","BBBBB"]);
}