轉(zhuǎn):http://www.cnblogs.com/zjoch/p/6018229.html
目前能夠?qū)崿F(xiàn)熱更新的方法,總結(jié)起來有以下三種
1. 使用FaceBook 的開源框架 reactive native,使用js寫原生的iOS應(yīng)用
ios app可以在運(yùn)行時從服務(wù)器拉取最新的js文件到本地,然后執(zhí)行,因?yàn)閖s是一門動態(tài)的
腳本語言,所以可以在運(yùn)行時直接讀取js文件執(zhí)行,也因此能夠?qū)崿F(xiàn)ios的熱更新
2. 使用lua 腳本。lua腳本如同js 一樣,也能在動態(tài)時被。之前憤怒的小鳥使用
lua腳本做的一個插件 wax,可以實(shí)現(xiàn)使用lua寫ios應(yīng)用。熱更新時,從服務(wù)器拉去lua腳本
然后動態(tài)的執(zhí)行就可以了。遺憾的是 wax目前已經(jīng)不更新了。
上面是網(wǎng)上現(xiàn)在能夠搜到的熱更新方法。
xcode 6 之后,蘋果開放了 ios 的動態(tài)庫編譯權(quán)限。所謂的動態(tài)庫,其實(shí)就是可以在運(yùn)行時加載。
正好利用這一個特性,用來做ios的熱更新。
1.
建立一個動態(tài)庫,如圖:

動態(tài)庫包含需要使用的viewCOntroller,當(dāng)然可以包含任何需要使用的自定義ui和邏輯。
動態(tài)庫的入口是一個jkDylib的類。它的.h和.m文件分別如下:
- //
- // JKDylib.h
- // JKDylb
- //
- // Created by wangdan on 15/7/5.
- // Copyright (c) 2015年 wangdan. All rights reserved.
- //
-
- #import <Foundation/Foundation.h>
-
- @interface JKDylib : NSObject
-
- -(void)showViewAfterVC:(id)fromVc inBundle:(NSBundle*)bundle;
-
- @end
.m文件
- //
- // JKDylib.m
- // JKDylb
- //
- // Created by wangdan on 15/7/5.
- // Copyright (c) 2015年 wangdan. All rights reserved.
- //
-
- #import "JKDylib.h"
- #import "JKViewController.h"
-
- @implementation JKDylib
-
- -(void)showViewAfterVC:(id)fromVc inBundle:(NSBundle*)bundle
- {
- if (fromVc == nil ) {
- return;
- }
-
- JKViewController *vc = [[JKViewController alloc] init];
- UIViewController *preVc = (UIViewController *)fromVc;
-
- if (preVc.navigationController) {
- [preVc.navigationController pushViewController:vc animated:YES];
- }
- else {
- UINavigationController *navi = [[UINavigationController alloc] init];
- [navi pushViewController:vc animated:YES];
- }
-
- }
- @end
上述代碼意圖非常明顯,
就是調(diào)用該動態(tài)庫的時候
- -(void)showViewAfterVC:(id)fromVc inBundle:(NSBundle*)bundle
在該函數(shù)中,創(chuàng)建一個viewController 然后使用mainBundler 的navigationController push 新建的viewController,顯示動態(tài)庫的ui界面。
而動態(tài)庫中的JKViewController 內(nèi)容則可以根據(jù)需要隨便定義。
2. 完成上述動態(tài)庫的編譯工作后,現(xiàn)在需要做的就是在主工程中,寫一段加載該動態(tài)庫的代碼。
主工程目錄如下:

在最重要的viewCotrooler里面,定義了加載動態(tài)庫的方法:
- //
- // ViewController.m
- // DylibTest
- //
- // Created by wangdan on 15/7/5.
- // Copyright (c) 2015年 wangdan. All rights reserved.
- //
-
- #import "ViewController.h"
- #import "AFNetWorking.h"
-
- @interface ViewController ()
-
- @end
-
- @implementation ViewController
-
- - (void)viewDidLoad {
- [super viewDidLoad];
- self.view.backgroundColor = [UIColor whiteColor];
- self.title = @"bundle test";
-
- AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] init];
- manager.responseSerializer = [AFJSONResponseSerializer serializer];
- NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.baidu.com"]];
- [manager HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) {
- NSLog(@"request success");
- } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
- NSLog(@"request failure");
- }];
-
-
-
- UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 100, 100, 50)];
- btn.backgroundColor = [UIColor blueColor];
-
- [btn addTarget:self
- action:@selector(btnHandler)
- forControlEvents:UIControlEventTouchUpInside];
-
- [self.view addSubview:btn];
-
- NSString *document = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
-
- BOOL writeResult =
- [@"hellow" writeToFile:[NSString stringWithFormat:@"%@/%@",document,@"hello.plist"] atomically:YES encoding:NSUTF8StringEncoding error:nil];
-
- // Do any additional setup after loading the view, typically from a nib.
- }
-
-
- -(void)btnHandler
- {
-
- //AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] init];
- //manager.responseSerializer = [AFJSONResponseSerializer serializer];
- // NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.baidu.com"]];
- // [manager HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) {
- // NSLog(@"request success");
- // } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
- // NSLog(@"request failure");
- //}];
-
- NSString *documentDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
- NSString *bundlePath = [NSString stringWithFormat:@"%@/%@",documentDirectory,@"JKDylb.framework"];
-
- if (![[NSFileManager defaultManager] fileExistsAtPath:bundlePath]) {
- NSLog(@"file not exist ,now return");
- return;
- }
- NSBundle *bundle = [NSBundle bundleWithPath:bundlePath];
-
- if (!bundle || ![bundle load]) {
- NSLog(@"bundle load error");
- }
-
- Class loadClass = [bundle principalClass];
- if (!loadClass) {
- NSLog(@"get bundle class fail");
- return;
- }
- NSObject *bundleObj = [loadClass new];
- [bundleObj performSelector:@selector(showViewAfterVC:inBundle:) withObject:self withObject:bundle];
-
- NSString *framePath = [[NSBundle mainBundle] privateFrameworksPath];
- NSLog(@"framePath is %@",framePath);
-
- NSLog(@"file attri \n %@",bundle.localizations);
-
- // [bundleObj showViewAfterVC:self inBundle:bundle];
- }
-
- - (void)didReceiveMemoryWarning {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
-
- @end
viewController視圖中有一個按鈕,點(diǎn)擊按鈕后,從 document目錄下面找到動態(tài)庫(雖然此時document下并沒有動態(tài)庫),動態(tài)庫的名稱約定好味
JKDylib.framework
然后使用NSBundle 加載該動態(tài)庫,具體見代碼。
加載成功后,調(diào)用在動態(tài)庫中實(shí)現(xiàn)的方法
- [bundleObj performSelector:@selector(showViewAfterVC:inBundle:) withObject:self withObject:bundle];
-
編譯該工程,然后運(yùn)行到手機(jī)上,然后退出該程序
3. 打開itunes 然后將動態(tài)庫同步到剛才的測試工程目錄下。
4.在此打開測試工程程序,點(diǎn)擊button,則會發(fā)現(xiàn)能夠進(jìn)入在動態(tài)庫中定義的ui界面了。
上面工程的參考代碼 在
http://download.csdn.net/detail/j_akill/8891881
關(guān)于動態(tài)更新的思考:
采用動態(tài)庫方式實(shí)現(xiàn)熱更新其實(shí)還是有一個問題,就是如何在主工程和動態(tài)庫之間共享組建
比如網(wǎng)絡(luò)組件以及其他等等第三方組件。
目前我沒發(fā)現(xiàn)好方法,只能在動態(tài)庫和主工程之間分別添加并且編譯。