进阶iOS 开发 iOS Developer

JSPatch(热修复)的使用

2016-10-11  本文已影响209人  uniapp

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

http://www.jspatch.com/

按照官方文档,使用流程很简单.测试时,在工程内新建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(){
            
            },
            });

在mian.js文件中,通过defineClass方法分别覆盖了原ViewController类中的实例方法,类方法,分类方法,属性和block.
使用过程中需要注意一些细节问题:
1 JSPatch实现走的是http网络请求,需要在info.plist中进行ATS注入.

 <key>NSAppTransportSecurity</key>
    <dict>
        <key>NSAllowsArbitraryLoads</key>
        <true/>
    </dict>

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

https://github.com/zhudong10/JSPatchDemo.git
上一篇下一篇

猜你喜欢

热点阅读