热修复
iOS应用审核时间之长,只叫人不堪忍受;但是更让人捶胸的是,App好不容易上线了,结果发现上线的APP有明显的bug。真他NND,各种无语,各种不是滋味。这个时候会想有没有不发布新版本解决类似这些小Bug,其实网上搜索一下,已经有很多方案了。如JSPatch和WaxPatch。具体这两个方案的原理可以参考:http://www.jianshu.com/p/41ed877aa0cd 这里不细说了,本文使用的是JSPatch来完成App热修复模块的逻辑。
我认为下载完补丁能够修复Bug,同时还要防止补丁有bug,要能够对已经下载到本地的补丁进行删除重新修复。这样补丁就应该要有编号。
#import "PGBaseObj.h"
/**
补丁,用于热修复
*/
@interface PGPatchObject : PGBaseObj
/*
补丁ID
*/
@property(nonatomic, strong)NSString *mFixID;
/*
补丁js脚本
*/
@property(nonatomic, strong)NSString *mFixString;
@end
补丁模块作统一的管理(下载,缓存本地、删除等)
@interface PGPatchManager : NSObject
+ (PGPatchManager *)shareInstance;
/*
其实执行是 [JPEngine startEngine]
*/
- (void)startListen;
/*
执行本要的脚本
*/
- (void)executeLocalHot;
/*
从服务器上获取新的脚本
*/
- (void)getHotData;
@end
获取新的补丁时需将本地已经存在的补丁作入参告之服务器,请求到新补丁的处理逻辑如下:
static PGPatchManager *s_patchManager = nil;
@interface PGPatchManager ()<PGApiDelegate>
@property(nonatomic, strong)NSMutableArray *arrayHots;
@end
@implementation PGPatchManager
+ (PGPatchManager *)shareInstance
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
s_patchManager = [[PGPatchManager alloc] init];
s_patchManager.arrayHots = [[NSMutableArray alloc] init];
[s_patchManager.arrayHots addObjectsFromArray:[s_patchManager localHots]];
});
return s_patchManager;
}
- (void)startListen
{
[JPEngine startEngine];
}
- (void)getHotData
{
NSMutableArray *localHotIDs = [[NSMutableArray alloc] init];
for(PGPatchObject *obj in self.arrayHots)
{
[localHotIDs addObject:obj.mFixID];
}
//本地已经存在的补丁
NSString *ids = [NSString jsonStringWithArray:localHotIDs];
[PGRequestManager startPostClient:API_TYPE_PATCH param:@{@"fixIds":ids} target:self extendParam:nil];
}
- (NSMutableArray *)localHots
{
NSObject *obj = [PGCacheManager readCacheType:ECacheType_Hots];
if(obj != nil)
return (NSMutableArray *)obj;
else
return [[NSMutableArray alloc] init];
}
- (void)saveHotsToLocal
{
[PGCacheManager cacheData:self.arrayHots type:ECacheType_Hots];
}
- (void)executeLocalHot
{
for(PGPatchObject *obj in self.arrayHots)
{
[JPEngine evaluateScript:obj.mFixString];
}
}
- (void)executeHot:(NSArray *)array
{
for(PGPatchObject *obj in array)
{
[JPEngine evaluateScript:obj.mFixString];
}
}
- (void)addHot:(NSArray *)array
{
if(array == nil || array.count <= 0)
return;
for(PGPatchObject *newobj in array)
{
for(PGPatchObject *obj in self.arrayHots)
{
if([newobj.mFixID compare:obj.mFixID] != NSOrderedSame)
{
[self.arrayHots addObject:obj];
}
}
}
}
- (void)delHot:(NSArray *)array
{
if(array == nil || array.count <= 0)
return;
for(PGPatchObject *delobj in array)
{
for(PGPatchObject *obj in self.arrayHots)
{
if([delobj.mFixID compare:obj.mFixID] == NSOrderedSame)
{
[self.arrayHots removeObject:obj];
break;
}
}
}
}
#pragma mark -
- (void)dataRequestFinish:(PGResultObject *)resultObj apiType:(PGApiType)apiType
{
if(apiType == API_TYPE_PATCH)
{
if(resultObj.nCode == 0)
{
NSMutableDictionary *dic = (NSMutableDictionary *)resultObj.dataObject;
NSMutableArray *addarray = [dic objectForKey:@"add"];
NSMutableArray *delarray = [dic objectForKey:@"del"];
//执行新的补丁
[self executeHot:addarray];
//删除旧的补丁
[self delHot:delarray];
//添加新的补丁
[self addHot:addarray];
//保存新的补丁
[self saveHotsToLocal];
}
}
}
@end
基本逻辑就是这样,完善的话,需对补丁进行安全加密,网络传输过程,本地存储时最好都考虑一下安全性。我代码里面是没有作加密处理的,可别学我哦。
本也想画一个补丁的修复逻辑流程图,但发现前人已经有现成的,我就不画了,其实是我比较懒。流程图可参考:http://blog.csdn.net/zm53373581/article/details/50011521
上一节:消息推送
下一节:webView封装
网友评论