如何在原生工程中引入Cordova工程-for iOS 【转】
http://blog.csdn.net/e20914053/article/details/50170487
如今混合开发方兴未艾,有的项目可能一开始是原生开发的,后期需要加入混合开发,如将Cordova工程引进到原生工程中。那么该如何操作呢?下面就来描述一下步骤。
1、首先我们来创建一个Cordova工程,取名MyCordova。在创建Cordova工程之前,需要先安装Cordova,具体安装方法网上很多,在此不累述。
- cordova create MyCordova
进入MyCordova工程目录,其结构如下:
2、接下来添加iOS平台,添加命令如下:
- cordova platform add ios
该命令需要在MyCordova工程根目录下执行。执行成功后,我们进入MyCordova工程下的platforms目录下,我们发现它增加了一个名为ios的文件目录。
3、回到MyCordova工程根目录,运行刚才添加的ios工程。
- cordova run ios
运行效果如下:
4、通过xcode创建一个原生工程MyApp。如果原生工程已经存在,可以忽略此步骤。
5、将MyCordova工程中iOS下的CordovaLib文件夹和www文件夹拷贝到MyApp工程目录下。
MyCordova目录:
MyApp目录:
6、参看上图。删除CordovaLib下面的build文件夹,此文件夹是在执行cordova run
ios命令过程中产生的,如果你没有执行过该命令就不会产生这个文件夹。然后通过xcode的Add files to “MyApp”
...将CordovaLib.xcodeproj文件和www文件夹添加到MyApp工程中。注意,在添加www文件夹时要勾选Create
folder references。如下:
7、将MyCordova工程根目录下的config.xml也添加到MyApp工程中。至此,所需的文件拷贝添加工作已经完成,其文件结构如下:
下面开始对对MyApp工程进行配置工作。
8、选择MyApp工程的Build Settings->Other Linker Flags, 设置-Objc -all_load
9、选择MyApp工程的Build Phases->Target Dependencies添加CordovaLib
10、选择MyApp工程的Build Phases->Link Binary With Librarys添加libCordova.a、 MobileCoreServices.framework、AssetsLibrary.framework相关框架。
到此MyApp工程已经顺利导入MyCordova工程了,点击Product->Build编译通过。下面再来创建并弹出Cordova页面。
11、创建一个视图控制器CordovaViewController。
其中CordovaViewController.h文件内容如下:
- #import <Cordova/CDVViewController.h>
- #import <Cordova/CDVCommandDelegateImpl.h>
- #import <Cordova/CDVCommandQueue.h>
- @interface CordovaViewController : CDVViewController
- @end
- @interface CordovaCommandDelegate : CDVCommandDelegateImpl
- @end
- @interface CordovaCommandQueue : CDVCommandQueue
- @end
CordovaViewController.m文件内容如下:
- #import "CordovaViewController.h"
- @implementation CordovaViewController
- - (id)initWithNibName:(NSString*)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil
- {
- self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
- if (self) {
- // Uncomment to override the CDVCommandDelegateImpl used
- // _commandDelegate = [[MainCommandDelegate alloc] initWithViewController:self];
- // Uncomment to override the CDVCommandQueue used
- // _commandQueue = [[MainCommandQueue alloc] initWithViewController:self];
- }
- return self;
- }
- - (id)init
- {
- self = [super init];
- if (self) {
- // Uncomment to override the CDVCommandDelegateImpl used
- // _commandDelegate = [[MainCommandDelegate alloc] initWithViewController:self];
- // Uncomment to override the CDVCommandQueue used
- // _commandQueue = [[MainCommandQueue alloc] initWithViewController:self];
- }
- return self;
- }
- - (void)didReceiveMemoryWarning
- {
- [super didReceiveMemoryWarning];
- }
- #pragma mark View lifecycle
- - (void)viewWillAppear:(BOOL)animated
- {
- [super viewWillAppear:animated];
- }
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- }
- - (void)viewDidUnload
- {
- [super viewDidUnload];
- }
- #pragma mark UIWebDelegate implementation
- - (void)webViewDidFinishLoad:(UIWebView*)theWebView
- {
- theWebView.backgroundColor = [UIColor blackColor];
- return [super webViewDidFinishLoad:theWebView];
- }
- @end
- @implementation CordovaCommandDelegate
- #pragma mark CDVCommandDelegate implementation
- - (id)getCommandInstance:(NSString*)className
- {
- return [super getCommandInstance:className];
- }
- - (NSString*)pathForResource:(NSString*)resourcepath
- {
- return [super pathForResource:resourcepath];
- }
- @end
- @implementation CordovaCommandQueue
- - (BOOL)execute:(CDVInvokedUrlCommand*)command
- {
- return [super execute:command];
- }
- @end
12、为MyApp工程中的“进入Cordova”UIButton绑定事件方法,来弹出CordovaViewController视图控制器。运行效果如下:(左边为原生视图控制器,右边为弹出的CordovaViewController视图控制器)
是不是跟在MyCordova工程中通过cordova run ios命令运行出来的效果一样呢!
ok,至此原生工程导入Cordova工程的方法步骤全部结束。
如何在原生工程中引入Cordova工程-for iOS 【转】的更多相关文章
- maven ----> 子工程中引入父工程
创建父工程,打包方式指定为 pom <groupId>com.example</groupId> <artifactId>SleuthMain</artifa ...
- vue-cli 工程中引入jquery
在vue-cli 生成的工程中引入了jquery,记录一下.(模板用的webpack) 首先在package.json里的dependencies加入"jquery" : &quo ...
- [转载]android工程中引入另一个工程中的资源
原文地址:android工程中引入另一个工程中的资源作者:87fayuan 在项目中可能遇到这样的问题:项目过大,于是细分为N个子模块来做,每个模块都是不同的工程.涉及到activity传数据时,可以 ...
- 测试工程中引入Masonry记录
测试工程中需要引入Masonry,在进行添加新库时发现了几个问题,记录如下,方便有相同问题的朋友查找解决: 1,podfile中添加 pod ‘Masonry’ 后,pod install --v ...
- 现有工程中集成Cordova
cocoapods引入cordova源码 1.依赖Cordova和wk插件 pod 'Cordova' pod 'cordova-plugin-wkwebview-engine' 建立Cordova支 ...
- 【idea】idea如何在maven工程中引入jar包
在pom.xml文件中引入所有代码包后,项目右键--maven--reimport </dependencies>
- MFC在一个工程中启动其他工程的exe文件
说明:有的时候把两个工程合并,但是偷懒不想在工程中添加代码,所以想到了这个办法,仅限偷懒哈哈哈哈 方法:新建一个主程序,在主程序的界面中添加按钮,在按钮的程序代码中添加以下语句: void CMain ...
- 在Web工程中引入Jquery插件报错解决方案
在学习Jquery插件的时候,遇到一个问题就是新建web工程后在WebRoot下引入Jquery插件的时候报错,不知道为什么好纠结,但是项目能正常运行,后来找到解决方案,在这里给大家分享一下. 解决方 ...
- 如何在 Maven 工程中引入其他jar包 并生效?(以 Netty 为例)
1.到 Maven 官网 查找 相关 框架 https://mvnrepository.com/artifact/io.netty/netty-all/4.1.32.Final 2.把相关 Xml体 ...
随机推荐
- URAL 1970 J - 皇后像廣場 dfs
J - 皇后像廣場 题目连接: http://acm.hust.edu.cn/vjudge/contest/123332#problem/J Description Vova was walking ...
- CentOS 7 下编译安装lnmp之PHP篇详解
一.安装环境 宿主机=> win7,虚拟机 centos => 系统版本:centos-release-7-5.1804.el7.centos.x86_64 二.PHP下载 官网 http ...
- Bugzilla Error message: couldn't create child process: 720003: index.cgi
two steps is try to fix this issue. 1. Turn off the windowns firewall 2. Register the perl to the sy ...
- Java中static、final用法小结(转)
一.final 1.final变量: 当你在类中定义变量时,在其前面加上final关键字,那便是说,这个变量一旦被初始化便不可改变,这里不可改变的意思对基本类型来说是其值不可变,而对于对象变量来说其引 ...
- 读写分离,读写分离死锁解决方案,事务发布死锁解决方案,发布订阅死锁解决方案|事务(进程 ID *)与另一个进程被死锁在 锁 资源上,并且已被选作死锁牺牲品。请重新运行该事务
前言: 由于网站访问压力的问题,综合分析各种因素后结合实际情况,采用数据库读写分离模式来解决当前问题.实际方案中采用“事务发布”模式实现主数据库和只读数据库的同步,其中: 发布服务器1 ...
- LINUX 源码+内核所有参数说明
http://www.cnblogs.com/tolimit/p/5065761.html
- [Winform]默认以管理员身份运行程序
摘要 在使用setupfactory打包之后,想让程序默认以管理员身份运行,因为涉及到创建文件删除文件的操作,如果权限比较低的话,会出现没有权限操作的bug. 解决办法 在项目中找到app.manif ...
- 让AngularJS的controllers之间共享数据
如何让controller之间共享数据呢?大致是让不同controller中的变量指向同一个实例. 通过service创建一个存放共享数据的对象. .service("greeting&qu ...
- Silverlight for Windows Phone Toolkit
Silverlight Toolkit 是一套codeplex上的很好的代码库,它里面包含了很多常用的但是Silverlight本身并不包含的控件.文档等内容.现在Silverlight Toolki ...
- arcgis10.1安装出现1606错误怎么办?找不到盘符
HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\ESRIHKEY_LOCAL_MACHINE\SOFTWARE\ESRIHKEY_CURRENT_USER\Softwa ...