从零开始设计搭建ios App框架(十二)

热修复


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封装

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,596评论 25 708
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,259评论 4 61
  • 1. 种树的最佳时间是25年前。仅次于它的最佳时间是现在。 大概是我20岁的时候,大学时期的我正处于一个人生的迷茫...
    安易_阅读 3,493评论 32 110
  • 猫的尴尬-郑彦芳 我是一只爱吃胡萝卜的灰色猫,我不知道我是什么品种,妈妈在看弗洛伊德心理学说是为了我,怕我将来会变...
    我是彦芳阅读 118评论 0 0
  • 无聊时听首歌,这样的自我放松和调节方式可能比吃有机食品还健康吧,but——有些歌,真心搞不懂是在讲什么,今天...
    大李杜阅读 450评论 0 0