使用Cordova开发iOS混合应用之二插件
创建插件
1,在Plugins文件夹下面创建MyTestCDV
代码如下:
.h文件
#import@interface MyTestCDV : CDVPlugin
// js 调用本地的方法
- (void)myNativeFunction:(CDVInvokedUrlCommand*)command;
@end
.m文件
#import "MyTestCDV.h"
#import "SecondViewController.h"
@implementation MyTestCDV
- (void)myNativeFunction:(CDVInvokedUrlCommand*)command
{
if (command.arguments)
{
NSString *str = [command.arguments firstObject];
SecondViewController *vc = [[SecondViewController alloc]init];
[self.viewController presentViewController:vc animated:YES completion:^{
vc.str = str;
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"我是OC回传的参数!"];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];}];}
}
@end
2 创建js文件如下图
.js文件内容如下
cordova.define("cordova-plugin-mytest.abc", function(require, exports, module) {
var exec = require("cordova/exec");
function MyFunction(){
};
// 创建对象
var myObject = new MyFunction();
// 给对象添加方法
MyFunction.prototype.jsfunction= function(success,fail,params){
exec(success,fail,'octestCDV','myNativeFunction',params);
};
module.exports = myObject;
});
3 添加配置
config.xml文件修改如下
cordova_plugins.js
index.html文件
运行效果图
点击之后效果