美文网首页iOS - CordovaiOS 开发
添加cordova到现有iOS工程并实现自定义JS插件

添加cordova到现有iOS工程并实现自定义JS插件

作者: 大圣的如意棒 | 来源:发表于2016-09-07 17:47 被阅读842次

       使用命令行来创建cordova工程,虽然方便简单,但是它会拥有它自己固定的目录结构,使用起来不太方便。如果将cordova核心功能添加到我们现有工程中,作为一个子集来使用,会给开发者提供很大的扩展性和灵活性,下面将详细介绍如何将cordova添加到现有工程中,并实现自定义插件功能。

一.添加cordova到现有工程

1.首先创建一个OC基本工程FFDemo,删去部分代码后,其结构如下:

2.拷贝Cordova相关文件到FFDemo目录下,拷贝后目录结构如下:

3. 在现有工程中添加folder cordova,将CordovaLib.xcodeproj添加到FFDemo工程cordova目录中,右键选择Add Files To FFDemo:

4.同步骤3,将www文件夹以及config.xml添加到cordova目录下:

代码工程目录如下:

5. 选择工程的Build Settings->Other Links, 设置-Objc -all_load (这个一定要设置,否则运行报错)

6.选择Build Phases->New Run Script Phase,将新增New Run Script Phase命名为copy www directory:

7. Build Phases->Target Dependencies添加CordovaLib

8.Build Phases->Link Binary With Librarys添加libCordova.a

9.测试cordova的使用,在ViewController.m中添加一个按钮,点击加载cordova:

//

//ViewController.m

//FFDemo

//

//Created by yujinyu on 16/6/23.

//Copyright © 2016年yujinyu. All rights reserved.

//

#import"ViewController.h"

@interfaceViewController()

- (void)initNavBar;

- (void)initViews;

@end

@implementationViewController

- (void)viewDidLoad {

[superviewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

[selfinitNavBar];

[selfinitViews];

}

- (void)didReceiveMemoryWarning {

[superdidReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

#pragma mark -- custom methods

- (void)initNavBar

{

self.navigationItem.title=@"Firefly";

}

- (void)initViews

{

self.view.backgroundColor= [UIColorwhiteColor];

CGRectcontentFrame =self.view.frame;

CGRectbtnFrame =CGRectMake((contentFrame.size.width-160)/2.0,

contentFrame.origin.y+280,

160,

40);

UIButton*btn = [[UIButtonalloc]initWithFrame:btnFrame];

btn.backgroundColor= [UIColorgreenColor];

[btnsetTitle:@"点击加载cordova"forState:UIControlStateNormal];

[btnaddTarget:selfaction:@selector(loadCordova)forControlEvents:UIControlEventTouchUpInside];

[self.viewaddSubview:btn];

}

- (void)loadCordova

{

CDVViewController*cdvController = [[CDVViewControlleralloc]init];

[self.navigationControllerpushViewController:cdvControlleranimated:YES];

}

@end

10.运行工程,可以看到cordova可以正常运行:

二.在工程中添加自定义插件

1.在Cordova目录下添加folder Plugins,并添加我们自定义的插件,这里写了一个简单的获取系统信息的接口。

其中System.m实现如下:

//

//SystemInfo.m

//Helloworld

//

//Created by yujinyu on 16/4/20.

//

//

#import"SystemInfo.h"

@implementationSystemInfo

- (void)getSystemInfo:(CDVInvokedUrlCommand*)command

{

NSDictionary* deviceProperties = [selfdeviceProperties];

CDVPluginResult*pluginResult = [CDVPluginResultresultWithStatus:CDVCommandStatus_OKmessageAsDictionary:deviceProperties];

[self.commandDelegatesendPluginResult:pluginResultcallbackId:command.callbackId];

}

- (NSDictionary*)deviceProperties

{

NSMutableDictionary* devProps = [NSMutableDictionarydictionaryWithCapacity:2];

[devPropssetObject:@"Firefly_cordova_1.0"forKey:@"version"];

NSDictionary* devReturn = [NSDictionarydictionaryWithDictionary:devProps];

returndevReturn;

}

@end

2.www目录下新建plugins/cordova-plugin-systeminfo/www/System.js,编写可以被调用的接口;

其中:

cordova-plugin-systeminfo.systeminfo 为插件Id;

exports.getSystemInfo = function(success, error) {},定义可以被js函数getSystemInfo(success,error),success,error分别为成功和失败的回掉函数;

exec(success, error, "systemInfo", "getSystemInfo", []),具体的执行函数,其中第三个参数为插件类名称,第四个参数为插件类的函数名,第三个参数,为参数数组;

3.在config.xml配置文件中定义该插件,以供初始化时候加载该插件;

4.在cordova_plugin.js文件中配置该js插件;

其中:

字段file为插件的js文件路径;

字段id为步骤2中定义的插件Id,供系统调用;

plugingID为自定义插件id;

clobbers:描述字段,可自定义;

5.现在可以使用我们自定义的插件systeminfo,在index.js中调用该插件,成功后弹出提示框;

6.运行工程,可以看到加载index.html页面执行了自定义插件函数;

源代码见github:

https://github.com/cornyu/Firefly_cordova

相关文章

网友评论

    本文标题:添加cordova到现有iOS工程并实现自定义JS插件

    本文链接:https://www.haomeiwen.com/subject/pljvdttx.html