iOS原生项目嵌入Cordova

来源:互联网 发布:骚男辣条淘宝店网址 编辑:程序博客网 时间:2024/05/22 11:54

MAC OS  High Sierra系统版本10.13

Xcode版本Version 9.0.1 (9A1004)


对于iOS原生项目嵌入cordova的一些记录,废话不说,开始正题!

嵌入cordova,网上一顿搜,搜到的都很详细,但是运行出问题,自己总结后记录下来,方便以后查阅,开始上手正式操作一遍。

首先需要创建一个cordova工程,cordova中文网,上面有很详细的教程,网址http://cordova.axuer.com/docs/zh-cn/latest/guide/cli/index.html

如何集成Cordova组件以WebView形式集成到Native应用中去,从头到尾顺序如下:

1.新建Demo工程,加入我们已经存在名为Demo项目,目录结构如下:


2、打开你创建好的cordova工程,拷贝CordovaLib、www文件夹和confil.xml到Demo文件夹下

如果CordovaLib文件夹下有build文件夹删除掉,那是编译cordova工程时自动创建的,目录结构如下:




3、将CordovaLib.xcodeproje添加到demo工程中,右键选择Add Files To Demo



4、添加www文件夹到demo工程中,这里注意勾选Create folder references





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.Link Binary With Librarys添加libCordova.a, MobileCoreServices,AssetsLibrary


9、command + B 编译通过后创建视图控制器CordovaViewController,


这是.h与.m内容

#import <Cordova/CDVViewController.h>#import <Cordova/CDVCommandDelegateImpl.h>#import <Cordova/CDVCommandQueue.h>@interface CordovaViewController : CDVViewController@end@interface CordovaCommandDelegate : CDVCommandDelegateImpl@end@interface CordovaCommandQueue : CDVCommandQueue@end  
#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
10、然后是APPdelegate.m里面设置,不然还是跳转到ViewController

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    // Override point for customization after application launch.        CordovaViewController *cord = [[CordovaViewController alloc]init];    self.window.rootViewController = cord;    [self.window makeKeyWindow];        return YES;}


11、运行工程,就会出现出图所示



至此嵌入cordova就结束了


阅读全文
0 0
原创粉丝点击