您的当前位置:首页正文

JSPatch(热修复)的使用

来源:图艺博知识网

当App上线后,难免不会出现bug.由于AppStore需要审核的原因,出现的bug就无法及时修复.而JSPatch可以实现在App启动时,自动发送网络请求补丁,替换自身的方法或属性,因此可以用来在线上紧急修复bug.
下面是官方地址:


按照官方文档,使用流程很简单.测试时,在工程内新建main.js文件.下面是我写的Demo的源码.
在ViewController类的.m文件中的代码如下:

#import "ViewController.h"
#import "TestCell.h"
#import "ViewController+CategoryTest.h"

@interface ViewController ()
@property (nonatomic,assign) NSInteger count;
@property (nonatomic,strong) NSString *name;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.tableView registerClass:[TestCell class] forCellReuseIdentifier:@"TestCell"];
    self.tableView.estimatedRowHeight = 100;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    [ViewController testLogOne];
    [ViewController testLogTwo];
    
    NSLog(@"*******count%zd**********", _count);
    
    [self blockTest:^(NSString *str) {
        NSLog(@"*******%@******",str);
    }];
}

- (void)blockTest: (void (^)(NSString *))block{
    block(@"blockTest");
}
+ (void)testLogOne{
    NSLog(@"*******classTestLogOne*******");
}

+ (void)testLogTwo{
    NSLog(@"*******classTestLogTwo*******");
}

- (void)testLog{
    NSLog(@"*******testLog*******");
    [self categoryTest];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    TestCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"TestCell"];
    if (!cell) {
        cell = [[TestCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"TestCell"];
    }
    return cell;
}
@end

下面是main.js中的代码:

//defineClass("类名", [属性],{实例方法},{类方法})
defineClass("ViewController",[],{
            
            tableView_numberOfRowsInSection: function(tableView, indexPath){
            //            console.log('***********');
            return 1;
            },
            },{})
//调用未覆盖前的实例方法
defineClass("ViewController", {
            viewDidLoad: function() {
            self.ORIGviewDidLoad();
            self.testLog();
            },
            })
//覆盖类方法
defineClass("ViewController", {}, {
            testLogOne: function(){
            console.log('*****ModifyClassFunctionOne******');
            },
            testLogTwo: function(){
            console.log('*****ModifyClassFunctionTwo******');
            }
            })
//覆盖分类方法
defineClass("ViewController",{
            categoryTest: function(){
            console.log('*****ModifycategoryTest******');
            },
            })
//修改属性
defineClass("ViewController", {
            viewDidLoad: function() {
            self.ORIGviewDidLoad();
            self.setValue_forKey(10,"_count");
            var num = 10;
            console.log('%d ******',self.count());
            },
            })
//修改block
defineClass("ViewController", {
            blockTest: function(){
            
            },
            });
 <key>NSAppTransportSecurity</key>
    <dict>
        <key>NSAllowsArbitraryLoads</key>
        <true/>
    </dict>

2 如果按步骤操作后,效果无法实现,需要将XCode退出后尝试.
下面是Demo的github链接:


Top