IOS 熱更新,實(shí)時(shí)代碼更新狭园,動(dòng)態(tài)更新读处,動(dòng)態(tài)庫(kù)framework
只要審核通過(guò)后,無(wú)須再次審核唱矛,就可以動(dòng)態(tài)更新罚舱,iOS有三種處理方案:
一、開(kāi)源框架 reactive native绎谦,但是編程語(yǔ)言是js
二管闷、lua 腳本
三、使用OC語(yǔ)言??的動(dòng)態(tài)庫(kù)framework窃肠。
前兩者包个,我不打算細(xì)講,我主要介紹怎么用oc進(jìn)行熱更新
1冤留、創(chuàng)建framework工程
2碧囊、代碼處理:
寫一個(gè)controller的控制工具類:
//??HotUpdateControl.m
//??HotUpdateMudel
//
//??Created?by?wukong?on?15/12/18.
//??Copyright???2015年?lhc.?All?rights?reserved.
//
#import?"HotUpdateControl.h"
#import?"AController.h"
#import?"BViewController.h"
#import?"CViewController.h"
#import?"DViewController.h"
#import?"EViewController.h"
@implementationHotUpdateControl
-(NSArray*)getVcs?{
return@[
[[AControlleralloc]init],
[[BViewControlleralloc]init],
[[CViewControlleralloc]init],
[[DViewControlleralloc]init],
[[EViewControlleralloc]init]];
}
@end
好了,開(kāi)始打包framework,為了以免打包出來(lái)的framework,在真機(jī)上面運(yùn)行不了纤怒,我們使用一個(gè)腳本來(lái)進(jìn)行打包糯而,目的是多型號(hào)CPU核心的合成,就是打出一個(gè)通用的包泊窘。
# Sets the target folders and the final framework product.
#?如果工程名稱和Framework的Target名稱不一樣的話熄驼,要自定義FMKNAME
#?例如:?FMK_NAME?=?"MyFramework"
FMK_NAME=${PROJECT_NAME}
#?Install?dir?will?be?the?final?output?to?the?framework.
#?The?following?line?create?it?in?the?root?folder?of?the?current?project.
INSTALL_DIR=${SRCROOT}/Products/${FMK_NAME}.framework
#?Working?dir?will?be?deleted?after?the?framework?creation.
WRK_DIR=build
DEVICE_DIR=${WRK_DIR}/Release-iphoneos/${FMK_NAME}.framework
SIMULATOR_DIR=${WRK_DIR}/Release-iphonesimulator/${FMK_NAME}.framework
#?-configuration?${CONFIGURATION}
#?Clean?and?Building?both?architectures.
xcodebuild?-configuration"Release"-target"${FMK_NAME}"-sdk?iphoneos?clean?build
xcodebuild?-configuration"Release"-target"${FMK_NAME}"-sdk?iphonesimulator?clean?build
#?Cleaning?the?oldest.
if[?-d"${INSTALL_DIR}"]
then
rm?-rf"${INSTALL_DIR}"
fi
mkdir?-p"${INSTALL_DIR}"
cp?-R"${DEVICE_DIR}/""${INSTALL_DIR}/"
#?Uses?the?Lipo?Tool?to?merge?both?binary?files?(i386?+?armv6/armv7)?into?one?Universal?final?product.
lipo?-create"${DEVICE_DIR}/${FMK_NAME}""${SIMULATOR_DIR}/${FMK_NAME}"-output"${INSTALL_DIR}/${FMK_NAME}"
rm?-r"${WRK_DIR}"
open"${INSTALL_DIR}"
3、建立一個(gè)主項(xiàng)目烘豹,就是使用這些動(dòng)態(tài)庫(kù)的工程
現(xiàn)在進(jìn)行讀取離線包的測(cè)試瓜贾,只要這個(gè)項(xiàng)目,能夠從沙箱里面讀取到代碼文件携悯,就意味著可以在線更新代碼阐虚,遠(yuǎn)程升級(jí)!0雎薄实束!
動(dòng)態(tài)庫(kù)已經(jīng)加載到了沙箱~~~
我修改了UITabBarController加載版塊的初始化方法,如果沙箱有framework動(dòng)態(tài)庫(kù)逊彭,就加載framework動(dòng)態(tài)庫(kù)上面的版塊咸灿,令到項(xiàng)目可以模塊化
//? TabController.m
//??HotUpdate
//
//??Created?by?wukong?on?15/12/18.
//??Copyright???2015年?lhc.?All?rights?reserved.
//
#import?"TabController.h"
//#import?
@interfaceTabController?()
@end
@implementationTabController
-(instancetype)initWithCoder:(NSCoder*)aDecoder{
if(self=?[superinitWithCoder:aDecoder])?{
NSString*documentDirectory?=?[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,?NSUserDomainMask,YES)lastObject];
NSArray*?arrFramework?=?[selfgetFilenamelistOfType:@"framework"fromDirPath:documentDirectory];
NSLog(@"%@",arrFramework);
if(arrFramework.count==0)?{
NSArray*?arrTitle?=?@[@"首頁(yè)",@"廣場(chǎng)",@"朋友圈",@"我的",@"設(shè)置"];
NSMutableArray*?arrVcs?=?@[].mutableCopy;
for(inti=0;?i
UIViewController*?vcRoot?=?[[UIViewControlleralloc]init];
vcRoot.title=?arrTitle[i];
vcRoot.view.backgroundColor=?[UIColorwhiteColor];
UINavigationController*?navi?=?[[UINavigationControlleralloc]initWithRootViewController:vcRoot];
[arrVcsaddObject:navi];
}
[selfsetViewControllers:arrVcsanimated:YES];
}else{
NSString*bundlePath?=?[NSStringstringWithFormat:@"%@/%@",documentDirectory,[arrFrameworklastObject]];
if(![[NSFileManagerdefaultManager]fileExistsAtPath:bundlePath])?{
NSLog(@"file?not?exist?,now??return");
returnself;
}
NSBundle*bundle?=?[NSBundlebundleWithPath:bundlePath];
if(!bundle?||?![bundleload])?{
NSLog(@"bundle?load?error");
}
Class?loadClass?=?[bundleclassNamed:@"HotUpdateControl"];
if(!loadClass)?{
NSLog(@"get?bundle?class?fail");
returnself;
}
NSObject*bundleObj?=?[loadClassnew];
NSArray*?arrVc?=?[bundleObjperformSelector:@selector(getVcs)];
NSMutableArray*?arrVcs?=?@[].mutableCopy;
for(inti=0;?i
UIViewController*?vcRoot?=arrVc[i];
vcRoot.view.backgroundColor=?[UIColorwhiteColor];
UINavigationController*?navi?=?[[UINavigationControlleralloc]initWithRootViewController:vcRoot];
[arrVcsaddObject:navi];
}
[selfsetViewControllers:arrVcsanimated:YES];
}
}
returnself;
}
-(NSArray*)getFilenamelistOfType:(NSString*)typefromDirPath:(NSString*)dirPath
{
NSArray*fileList?=?[[[NSFileManagerdefaultManager]contentsOfDirectoryAtPath:dirPatherror:nil]
pathsMatchingExtensions:[NSArrayarrayWithObject:type]];
returnfileList;
}
-?(void)viewDidLoad?{
[superviewDidLoad];
}
@end
看結(jié)果了,如果是本地的默認(rèn)版本侮叮,應(yīng)該是
@[@"首頁(yè)",@"廣場(chǎng)",@"朋友圈",@"我的",@"設(shè)置"];的模塊
但是如果是沙箱里面的模塊
那么就應(yīng)該ABCDE
good luck避矢!
原文鏈接http://blog.csdn.net/jianrenbubai/article/details/50351507?