使用Cordova开发iOS混合应用之二插件

创建插件

1,在Plugins文件夹下面创建MyTestCDV

使用Cordova开发iOS混合应用之二插件_第1张图片

代码如下:

.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文件如下图


使用Cordova开发iOS混合应用之二插件_第2张图片


.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开发iOS混合应用之二插件_第3张图片

cordova_plugins.js

使用Cordova开发iOS混合应用之二插件_第4张图片

index.html文件

使用Cordova开发iOS混合应用之二插件_第5张图片


运行效果图

使用Cordova开发iOS混合应用之二插件_第6张图片

点击之后效果


使用Cordova开发iOS混合应用之二插件_第7张图片

demo地址

你可能感兴趣的:(使用Cordova开发iOS混合应用之二插件)