使用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文件


运行效果图

点击之后效果


demo地址

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容