http://blog.csdn.net/e20914053/article/details/50170487

如今混合开发方兴未艾,有的项目可能一开始是原生开发的,后期需要加入混合开发,如将Cordova工程引进到原生工程中。那么该如何操作呢?下面就来描述一下步骤。

1、首先我们来创建一个Cordova工程,取名MyCordova。在创建Cordova工程之前,需要先安装Cordova,具体安装方法网上很多,在此不累述。

  1. cordova create MyCordova

进入MyCordova工程目录,其结构如下:

2、接下来添加iOS平台,添加命令如下:

  1. cordova platform add ios

该命令需要在MyCordova工程根目录下执行。执行成功后,我们进入MyCordova工程下的platforms目录下,我们发现它增加了一个名为ios的文件目录。

3、回到MyCordova工程根目录,运行刚才添加的ios工程。

  1. 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文件内容如下:

  1. #import <Cordova/CDVViewController.h>
  2. #import <Cordova/CDVCommandDelegateImpl.h>
  3. #import <Cordova/CDVCommandQueue.h>
  4. @interface CordovaViewController : CDVViewController
  5. @end
  6. @interface CordovaCommandDelegate : CDVCommandDelegateImpl
  7. @end
  8. @interface CordovaCommandQueue : CDVCommandQueue
  9. @end

CordovaViewController.m文件内容如下:

  1. #import "CordovaViewController.h"
  2. @implementation CordovaViewController
  3. - (id)initWithNibName:(NSString*)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil
  4. {
  5. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  6. if (self) {
  7. // Uncomment to override the CDVCommandDelegateImpl used
  8. // _commandDelegate = [[MainCommandDelegate alloc] initWithViewController:self];
  9. // Uncomment to override the CDVCommandQueue used
  10. // _commandQueue = [[MainCommandQueue alloc] initWithViewController:self];
  11. }
  12. return self;
  13. }
  14. - (id)init
  15. {
  16. self = [super init];
  17. if (self) {
  18. // Uncomment to override the CDVCommandDelegateImpl used
  19. // _commandDelegate = [[MainCommandDelegate alloc] initWithViewController:self];
  20. // Uncomment to override the CDVCommandQueue used
  21. // _commandQueue = [[MainCommandQueue alloc] initWithViewController:self];
  22. }
  23. return self;
  24. }
  25. - (void)didReceiveMemoryWarning
  26. {
  27. [super didReceiveMemoryWarning];
  28. }
  29. #pragma mark View lifecycle
  30. - (void)viewWillAppear:(BOOL)animated
  31. {
  32. [super viewWillAppear:animated];
  33. }
  34. - (void)viewDidLoad
  35. {
  36. [super viewDidLoad];
  37. }
  38. - (void)viewDidUnload
  39. {
  40. [super viewDidUnload];
  41. }
  42. #pragma mark UIWebDelegate implementation
  43. - (void)webViewDidFinishLoad:(UIWebView*)theWebView
  44. {
  45. theWebView.backgroundColor = [UIColor blackColor];
  46. return [super webViewDidFinishLoad:theWebView];
  47. }
  48. @end
  49. @implementation CordovaCommandDelegate
  50. #pragma mark CDVCommandDelegate implementation
  51. - (id)getCommandInstance:(NSString*)className
  52. {
  53. return [super getCommandInstance:className];
  54. }
  55. - (NSString*)pathForResource:(NSString*)resourcepath
  56. {
  57. return [super pathForResource:resourcepath];
  58. }
  59. @end
  60. @implementation CordovaCommandQueue
  61. - (BOOL)execute:(CDVInvokedUrlCommand*)command
  62. {
  63. return [super execute:command];
  64. }
  65. @end

12、为MyApp工程中的“进入Cordova”UIButton绑定事件方法,来弹出CordovaViewController视图控制器。运行效果如下:(左边为原生视图控制器,右边为弹出的CordovaViewController视图控制器)

                        

是不是跟在MyCordova工程中通过cordova run ios命令运行出来的效果一样呢!

ok,至此原生工程导入Cordova工程的方法步骤全部结束。

如何在原生工程中引入Cordova工程-for iOS 【转】的更多相关文章

  1. maven ----> 子工程中引入父工程

    创建父工程,打包方式指定为 pom <groupId>com.example</groupId> <artifactId>SleuthMain</artifa ...

  2. vue-cli 工程中引入jquery

    在vue-cli 生成的工程中引入了jquery,记录一下.(模板用的webpack) 首先在package.json里的dependencies加入"jquery" : &quo ...

  3. [转载]android工程中引入另一个工程中的资源

    原文地址:android工程中引入另一个工程中的资源作者:87fayuan 在项目中可能遇到这样的问题:项目过大,于是细分为N个子模块来做,每个模块都是不同的工程.涉及到activity传数据时,可以 ...

  4. 测试工程中引入Masonry记录

    测试工程中需要引入Masonry,在进行添加新库时发现了几个问题,记录如下,方便有相同问题的朋友查找解决:   1,podfile中添加 pod ‘Masonry’ 后,pod install --v ...

  5. 现有工程中集成Cordova

    cocoapods引入cordova源码 1.依赖Cordova和wk插件 pod 'Cordova' pod 'cordova-plugin-wkwebview-engine' 建立Cordova支 ...

  6. 【idea】idea如何在maven工程中引入jar包

    在pom.xml文件中引入所有代码包后,项目右键--maven--reimport </dependencies>

  7. MFC在一个工程中启动其他工程的exe文件

    说明:有的时候把两个工程合并,但是偷懒不想在工程中添加代码,所以想到了这个办法,仅限偷懒哈哈哈哈 方法:新建一个主程序,在主程序的界面中添加按钮,在按钮的程序代码中添加以下语句: void CMain ...

  8. 在Web工程中引入Jquery插件报错解决方案

    在学习Jquery插件的时候,遇到一个问题就是新建web工程后在WebRoot下引入Jquery插件的时候报错,不知道为什么好纠结,但是项目能正常运行,后来找到解决方案,在这里给大家分享一下. 解决方 ...

  9. 如何在 Maven 工程中引入其他jar包 并生效?(以 Netty 为例)

    1.到 Maven 官网  查找 相关 框架 https://mvnrepository.com/artifact/io.netty/netty-all/4.1.32.Final 2.把相关 Xml体 ...

随机推荐

  1. 喵哈哈村的魔法考试 Round #5 (Div.2) 题解

    老规矩 有问题直接联系我:475517977@qq.com A 直接暴力的for一遍,统计连续的有多少个就好了.模拟题. #include<bits/stdc++.h> using nam ...

  2. STM32 F4 GPIO Modes

    STM32 F4 GPIO Modes Goal: creating a visual summary of GPIO configuration modes. The summary at the ...

  3. 打通Linux脉络系列:进程、线程和调度

    http://edu.csdn.net/huiyiCourse/series_detail/60 http://edu.csdn.net/mycollege

  4. [Winform]Media Player com组件应用中遇到的问题

    摘要 最近一个项目中,需要用到在客户端全屏循环播放视频,当时考虑使用开源的播放器,但控制起来不方便,然后考虑既然都是windows系统,那么可以考虑使用微软自带的Media Player播放器.所以在 ...

  5. AngularJS中实现显示或隐藏动画效果的3种方式

    本篇体验在AngularJS中实现在"显示/隐藏"这2种状态切换间添加动画效果. 通过CSS方式实现显示/隐藏动画效果 思路: →npm install angular-anima ...

  6. 理解 HTTPS 协议

    英文原文:Understanding HTTPS Protocol 最近我们看到很多站点使用 HTTPS 协议提供网页服务.通常情况下我们都是在一些包含机密信息的站点像银行看到 HTTPS 协议. 如 ...

  7. [LNU.Machine Learning.Question.1]梯度下降方法的一些理解

    曾经学习machine learning,在regression这一节,对求解最优化问题的梯度下降方法,理解总是处于字面意义上的生吞活剥. 对梯度的概念感觉费解?到底是标量还是矢量?为什么沿着负梯度方 ...

  8. 一篇linux的通讯文章

    今年的linux内核开发大会上,google的开发人员也上台做了名为“how google use linux”的演讲.我斗胆翻译注解一番――括号内为注解,欢迎读者斧正. 原文链接参见:http:// ...

  9. 深入理解Java并发之synchronized实现原理

    深入理解Java类型信息(Class对象)与反射机制 深入理解Java枚举类型(enum) 深入理解Java注解类型(@Annotation) 深入理解Java类加载器(ClassLoader) 深入 ...

  10. TextView中文文档

    十分感谢农民伯伯的翻译:http://www.cnblogs.com/over140/archive/2010/08/27/1809745.html xml 属性: 属性名称 描述  android: ...