1. 程式人生 > >iOS原生專案嵌入Cordova

iOS原生專案嵌入Cordova

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就結束了

2018年1月19號,對於cordova補充

上面寫的是從cordova工程中吧對應cordova的程式碼扒下來放在原生工程裡,還要一個是使用cocoapods,直接搜尋cordova新增進去就可以正常使用了,不用上面那些繁瑣的步驟