iOS 利用JSPatch 添加热补丁功能
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 替换原来的界面
等等,更多功能,有待研究
有问题可以写评论哦,
iOS 利用JSPatch 添加热补丁功能相关推荐
- html添加友情链接,WordPress利用css添加“友情链接”功能
使用WordPress建站的话,一般都需要添加友友情链接,它在wordpress站点中,一般都是通过"小工具"来插入实现的.有的主题自带"友情链接"小工具,但也 ...
- kpatch 内核热补丁几个使用问题记录
kpatch 内核热补丁相关问题记录 相关工具版本信息 内核版本:linux-3.16.35 kpatch 版本:0.8.0 源码地址:https://github.com/dynup/kpatch ...
- Android热补丁的一点总结
由于项目需要,我研究热补丁的实现是从12月上旬开始的,那时候我还是个对编译.ant.hudson只闻其名,对javassist.groovy闻所未闻的孩子:而现在,我已经是一个对ant.hudson一 ...
- QCon速递:Xen漏洞热补丁修复、异地双活、ODPS新功能与金融互联网
QCon速递:Xen漏洞热补丁修复.异地双活.ODPS新功能与金融互联网
- 【腾讯Bugly干货分享】QFix探索之路—手Q热补丁轻量级方案
本文来自于腾讯bugly开发者社区,非经作者同意,请勿转载,原文地址:http://dev.qq.com/topic/57ff5832bb8fec206ce2185d 导语 QFix 是手Q团队近期推 ...
- Lua快速入门篇(XLua教程)(Yanlz+热更新+xLua+配置+热补丁+第三方库+API+二次开发+常见问题+示例参考)
<Lua热更新> ##<Lua热更新>发布说明: ++++"Lua热更新"开始了,立钻哥哥终于开始此部分的探 ...
- 【腾讯bugly干货分享】微信Android热补丁实践演进之路
为什么80%的码农都做不了架构师?>>> 本文来自于腾讯bugly开发者社区,非经作者同意,请勿转载,原文地址:http://bugly.qq.com/bbs/forum.ph ...
- (4.2.32)各大热补丁方案分析和比较
选自: [腾讯bugly干货分享]微信Android热补丁实践演进之路 各大热补丁方案分析和比较 继插件化后,热补丁技术在2015年开始爆发,目前已经是非常热门的Android开发技术.其中比较著名的 ...
- 【腾讯Bugly干货分享】微信热补丁Tinker的实践演进之路
本文来自于腾讯bugly开发者社区,非经作者同意,请勿转载,原文地址:http://dev.qq.com/topic/57ad7a70eaed47bb2699e68e Dev Club 是一个交流移动 ...
最新文章
- ASP.NET 2.0 ajax中gridView的刷新问题!
- Redis 生产架构选型解决方案
- linux rpm和deb的区别,rpm与deb的区别
- java 压缩文件夹_java 实现压缩文件(单文件 或 文件夹)
- SVN在vs2013中使用
- 因为这几个TypeScript代码的坏习惯,同事被罚了500块
- IOT(20)---2018年有哪些值得期待的物联网应用领域?
- (整理)REHL6.5_Yum安装Reids
- NoSQL数据库:从故障中恢复数据的原理
- SQLite 时间格式化
- 关于Arduino下载并添加DHT11库文件的详细教程
- AABB包围盒和OBB包围盒区别
- tewa600agm是千兆吗_请问电信天翼网关光纤猫超级用户 型号tewa-600aem/tewa600agm
- python时间计算天数差
- 【国产开源】兼容redis协议的内存数据库
- 当linux reboot不成功时怎么办
- vue3使用setup代替created
- js中条件三目运算符?:
- 正兴证券损失通知书:Rosen Law Firm宣布针对中国正兴车轮集团有限公司提起证券集体诉讼和参加集体诉讼的重要截止日期
- 汇编语言程序设计V-贺利坚-专题视频课程