iOS 利用JSPatch 添加热补丁功能

LaurelFrank 9年前

来自: http://www.cnblogs.com/machao/p/5198555.html

ios 由于苹果的审核政策,一旦上线后发现bug是件让人崩溃的事情

不过可以利用oc的runtime机制可以家用JSPatch动态的为工程打热补丁

下载地址:https://github.com/agelessman/JSPatch.git

如果不用cocoapods导入的话,不需要修改,如果拖到工程的,需要改头文件,

例如: #import “abc.h”

在appdelegate中添加类似下边的方法,写一个本地的属性记录补丁的版本号,如果文件存在,再调用

 1 - (void)hotfix {   2        3     // 获得应用程序沙盒的Downloads文件夹路径   4        5     QKYGuideAccount *guideAccount = [QKYAccountTool guideAccount];   6        7     NSArray *arrDownloadPaths =  NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);   8     NSString *loadPathsPath=[arrDownloadPaths objectAtIndex:0];   9     NSString *patchPath = [loadPathsPath stringByAppendingPathComponent:[NSString stringWithFormat:@"patch_%@.js",guideAccount.patchVersion.length ? guideAccount.patchVersion : @"0"]];  10     NSFileManager *fileManager = [NSFileManager defaultManager];  11     BOOL isdir = NO;  12     if ([fileManager fileExistsAtPath:patchPath isDirectory:&isdir]) {  13         [JPEngine startEngine];  14         [JPEngine evaluateScript:[NSString stringWithContentsOfFile:patchPath encoding:NSUTF8StringEncoding error:nil]];  15     };  16       17     QKYLog(@"Downloads path: %@",patchPath);  18 }

在控制器中添加下边的方法,目的就是发请求到服务器,获取是否更新,

 1    //处理热修复   2     self.dataController = [[QKYListDataController alloc] init];   3     [self.dataController getIsNeedHotfixResultWithSuccessBlock:^(QKYIsNeedHotfixResult *  _Nonnull success, BOOL last) {   4            5         if (success.code.integerValue == 1 && success.newpatch.integerValue == 1) {// 现在补丁   6             [self.dataController downloadpatchWithUrl:success.patchurl];   7         }   8            9           10     } errorMsgBlock:^(NSError * _Nullable error, id  _Nullable msgBody) {  11           12     }];  13       14     NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];  15     NSString *version            = [infoDictionary objectForKey:@"CFBundleShortVersionString"];  16     NSMutableDictionary *dicM = [NSMutableDictionary dictionary];  17     [dicM setValue:@"2" forKey:@"comefrom"];  18     [dicM setValue:version forKey:@"patchappversion"];  19     QKYGuideAccount *guide = [QKYAccountTool guideAccount];  20     [dicM setValue:guide.patchVersion.length ? guide.patchVersion : @"0" forKey:@"patchversion"];

需要说明的是这里的dataController 是一个模型,下载补丁的方法封装到了这个模型中

在下载的条件成熟的情况下,下载附件

 1 - (void)downloadpatchWithUrl:(NSString *)url {   2        3     if (!url) return;   4        5     NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];   6        7     AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];   8        9     manager.responseSerializer = [AFHTTPResponseSerializer serializer];  10       11     NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];  12       13     NSProgress *downloadProgress = nil;  14       15     NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:&downloadProgress destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {  16           17         NSURL *downloadURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];  18         return [downloadURL URLByAppendingPathComponent:[NSString stringWithFormat:@"patch_%@.js",self.result.patchversion]];  19           20     } completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {  21           22    23         if (error) {  24             return ;  25         }  26         QKYGuideAccount *account = [QKYAccountTool guideAccount];  27         account.patchVersion = self.result.patchversion;  28         [QKYAccountTool saveGuideAccount:account];  29           30         [appDelegate hotfix];  31           32           33     }];  34   35     [downloadTask resume];  36 }

下载成功后保存最新的补丁号到本地属性中,调用JSPatch,让刚下载的代码生效

需要特别说明的是,加载补丁文件,是有顺序的,例如0,1,2 而且补丁文件中使用的是js的代码,

能够帮助我的功能:

1 修复导致崩溃的错误

2 替换原来的方法

3.添加新的方法

4 替换原来的界面

等等,更多功能,有待研究

有问题可以写评论哦,