簡(jiǎn)介
聲明
該文章中的內(nèi)容并不能用于實(shí)際項(xiàng)目中.(因?yàn)檫@樣的玩法是被
Apple
禁止的).
當(dāng)然企業(yè)賬號(hào)沒(méi)人管,可以這么玩玩試試.
文內(nèi)也不包含什么高端技術(shù).僅僅是突然想到了就做做試試,純屬個(gè)人興趣,當(dāng)做閑暇時(shí)間的娛樂(lè).
補(bǔ)充: 模擬器玩玩就可以, 在真機(jī)上,目前常見(jiàn)系統(tǒng)版本,幾乎都已經(jīng)不允許動(dòng)態(tài)加載沙盒內(nèi)的動(dòng)態(tài)庫(kù)了.
目標(biāo):
實(shí)現(xiàn)像 PC
游戲維護(hù)更新一樣,每次更新至下載相關(guān)文件而無(wú)需重新全部下載.
應(yīng)用相關(guān)功能也全部由服務(wù)器提供下載更新部分內(nèi)容的動(dòng)態(tài)庫(kù)來(lái)實(shí)現(xiàn),而無(wú)需完全下載更新客戶端.
說(shuō)明:
工程共2個(gè)類:
-
Framework
類主要處理網(wǎng)絡(luò)相關(guān)內(nèi)容. -
ViewController
類作為視圖相關(guān)操作類.
內(nèi)容
Framework類
該類主要是負(fù)責(zé)處理服務(wù)器請(qǐng)求到的framework
相關(guān)信息的處理以及服務(wù)下載framework
相關(guān)庫(kù)等操作.
Framework
頭文件相關(guān)屬性
相關(guān)方法未作展示
#import <Foundation/Foundation.h>
@interface Framework : NSObject
// 服務(wù)器返回的相關(guān)字段
@property (nonatomic, strong) NSString *name; // 名稱
@property (nonatomic, strong) NSString *className; // 類名
@property (nonatomic, strong) NSString *type; // 類型
@property (nonatomic, strong) NSString *url; // 下載地址
@property (nonatomic, strong) NSString *version; // 版本號(hào)
// 本地需要使用到的屬性
@property (nonatomic, strong) NSString *fileName; // 文件名 (解壓后的 .framework 文件)
@property (nonatomic, strong) NSString *cachePath; // 下載文件緩存路徑
@property (nonatomic, strong) NSString *libPath; // 解壓后存放 fraework 文件夾路徑
@property (nonatomic, strong) NSString *loadPath; // framework 的完整路徑
@end
Framework
類實(shí)現(xiàn)
最常見(jiàn)的一類方法,把從服務(wù)器請(qǐng)求回來(lái)的數(shù)據(jù)轉(zhuǎn)換成 model
對(duì)象,方便相關(guān)操作
/**
根據(jù)字典創(chuàng)建對(duì)象
@param dictionary 對(duì)象信息的字典
@return framewor 信息對(duì)象
*/
+ (instancetype)frameworkWithDictionary:(NSDictionary *)dictionary {
Framework *lib = [[Framework alloc] init];
[lib setValuesForKeysWithDictionary:dictionary];
return lib;
}
/**
通過(guò)請(qǐng)求結(jié)果創(chuàng)建對(duì)象
@param data 請(qǐng)求數(shù)據(jù)
@return 信息對(duì)象數(shù)組
*/
+ (NSArray *)librarysWithRequestData:(NSArray *)data {
NSMutableArray *array = [NSMutableArray new];
for (NSDictionary *dic in data) {
[array addObject:[self frameworkWithDictionary:dic]];
}
return [array copy];
}
服務(wù)器獲取 frmework
相關(guān)信息
/**
獲取最新的 library 信息
@param success 成功回調(diào)
@param failure 失敗回調(diào)
*/
+ (void)frameworksInfoWithSuccess:(void(^)(NSArray *frameworks))success
failure:(void(^)(NSError *error))failure {
[[AFHTTPSessionManager manager] POST:@"http://172.16.30.197:8888/update/request.php" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"responseObject = %@", responseObject);
success([self librarysWithRequestData:responseObject[@"librarys"]]);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"error = %@", error);
failure(error);
}];
}
取到 frmework
相關(guān)信息后,通過(guò)服務(wù)器返回的地址下載對(duì)應(yīng) framework
動(dòng)態(tài)庫(kù)文件
解壓 zip 需要首先引入 #import <SSZipArchive.h>
- (void)downLoad:(void(^)(NSError * error))resultBlock {
NSURL *url = [NSURL URLWithString:_url];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSURLSessionDownloadTask *task =
[[AFHTTPSessionManager manager] downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) {
} destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
return [NSURL fileURLWithPath:self.cachePath];
} completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
if (!error) {
[SSZipArchive unzipFileAtPath:self.cachePath toDestination:self.libPath];
}
resultBlock(error);
}];
[task resume];
}
以下是 Getter
方法
文件的存放的路徑完全可以看自己的喜好
拼接 frmework
動(dòng)態(tài)庫(kù)文件,name
和type
擴(kuò)展名都有服務(wù)器返回
- (NSString *)fileName {
if (_fileName == nil) {
self.fileName = [NSString stringWithFormat:@"%@.%@", _name, _type];
}
return _fileName;
}
下載的 zip
文件的存放地址
因?yàn)橹苯酉螺d
.framework
文件到本地之后,是一個(gè)無(wú)后綴的文件.為了避免出現(xiàn)別的問(wèn)題直接由服務(wù)器提供zip
文件,另外資源文件做相關(guān)壓縮操作也可以減小文件大小,提升下載速度.
- (NSString *)cachePath {
if (_cachePath == nil) {
// 這里要返回一個(gè)NSURL阵幸,其實(shí)就是文件的位置路徑
NSString * path = [self frameworkDir];
path = [path stringByAppendingPathComponent:@"zip"];
if (![[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:nil]) {
[[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
}
// 使用建議的路徑
_cachePath = [path stringByAppendingPathComponent:self.fileName];
if ([[NSFileManager defaultManager] fileExistsAtPath:_cachePath]) {
[[NSFileManager defaultManager] removeItemAtPath:_cachePath error:nil];
}
}
return _cachePath;
}
zip
文件解壓后framework
存放的文件夾路徑
- (NSString *)libPath {
if (_libPath == nil) {
// 這里要返回一個(gè)NSURL屯伞,其實(shí)就是文件的位置路徑
NSString * path = [self frameworkDir];
path = [path stringByAppendingPathComponent:@"lib"];
if (![[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:nil]) {
[[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
}
// 使用建議的路徑
_libPath = path;
if ([[NSFileManager defaultManager] fileExistsAtPath:_libPath]) {
[[NSFileManager defaultManager] removeItemAtPath:_libPath error:nil];
}
}
return _libPath;
}
framework
動(dòng)態(tài)庫(kù)文件完整的路徑
- (NSString *)loadPath {
if (_loadPath == nil) {
NSString *path = [self.libPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", _name, _type]];
self.loadPath = [path stringByAppendingPathComponent:_name];
}
return _loadPath;
}
用于存放zip
和 framework
文件的上路文件夾路徑
- (NSString *)frameworkDir {
// 這里要返回一個(gè)NSURL,其實(shí)就是文件的位置路徑
NSString * path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
// 使用建議的路徑
path = [path stringByAppendingPathComponent:@"Framework"];
if (![[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:nil]) {
[[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
}
return path;
}
ViewController類
ViewController
是工程的主視圖控制器,同時(shí)也擔(dān)任了動(dòng)態(tài)庫(kù)的加載以及動(dòng)態(tài)庫(kù)入口類的實(shí)例化相關(guān)操作.
- (void)viewDidLoad {
[super viewDidLoad];
[self serverLibiaryVersion];
}
參數(shù)className
是已經(jīng)約定好的動(dòng)態(tài)庫(kù)內(nèi)部功能的入口類類名, 并且動(dòng)態(tài)庫(kù)里必須包含該類.否則相關(guān)操作也會(huì)失敗.
- (void)startAppWithClassName:(NSString *)className {
Class class = NSClassFromString(className);
UIViewController *vc = [[class alloc] init];;
[self addChildViewController:vc];
[self.view addSubview:vc.view];
}
獲取服務(wù)器目前提供的最新版本的動(dòng)態(tài)庫(kù).
當(dāng)前并未實(shí)現(xiàn)版本號(hào)等對(duì)比的邏輯,僅僅只是直接從服務(wù)器下載返回的版本信息指定的動(dòng)態(tài)庫(kù)并進(jìn)行加載運(yùn)行.相關(guān)原理大概就是這樣,此處可以作為后續(xù)研究.
/**
獲取服務(wù)器動(dòng)態(tài)庫(kù)版本信息
*/
- (void)serverLibiaryVersion {
[Framework frameworksInfoWithSuccess:^(NSArray *frameworks) {
[self loadFrameworks:frameworks];
} failure:^(NSError *error) {
NSLog(@"error = %@", error);
}];
}
根據(jù)服務(wù)器返回的動(dòng)態(tài)庫(kù)文件地址下載動(dòng)態(tài)庫(kù)文件
/**
下載動(dòng)態(tài)庫(kù)
*/
- (void)loadFrameworks:(NSArray *)frameworks {
Framework *framework = frameworks[0];
[framework downLoad:^(NSError *error) {
if (error) {
NSLog(@"error = %@", error);
}else {
[self dlopenLoadDylibWithPath:framework.loadPath];
[self startAppWithClassName:framework.className];
}
}];
}
加載本地動(dòng)態(tài)庫(kù)
加載本地動(dòng)態(tài)庫(kù) 首先需要引入 #import <dlfcn.h>
dlopen() 是一個(gè)計(jì)算機(jī)函數(shù)假褪,功能是以指定模式打開(kāi)指定的動(dòng)態(tài)鏈接庫(kù)文件,并返回一個(gè)句柄給dlsym()的調(diào)用進(jìn)程。使用dlclose() 來(lái)卸載打開(kāi)的庫(kù)。
通過(guò) framework
文件在本地的路徑加載該動(dòng)態(tài)庫(kù).
/**
通過(guò)路徑加載動(dòng)態(tài)庫(kù)
*/
- (BOOL)dlopenLoadDylibWithPath:(NSString *)path {
void * libHandle = NULL;
libHandle = dlopen([path cStringUsingEncoding:NSUTF8StringEncoding], RTLD_NOW);
if (libHandle == NULL) {
char *error = dlerror();
NSLog(@"dlopen error: %s", error);
dlclose(libHandle);
return NO;
} else {
NSLog(@"dlopen load framework success.");
dlclose(libHandle);
return YES;
}
}
未完待續(xù)
后續(xù)服務(wù)端以及最終效果見(jiàn):iOS動(dòng)態(tài)更新技術(shù)探索 (下)