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 替換原來的界面

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

有問題可以寫評論哦,


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM