美文网首页iOSiOS BlogiOS开发那些事
iOS 通过 JSPatch 实时修复线上 bug!

iOS 通过 JSPatch 实时修复线上 bug!

作者: X先生_vip | 来源:发表于2015-11-30 19:00 被阅读2502次

JSPatch 是一个开源项目(Github链接),只需要在项目里引入极小的引擎文件,就可以使用 JavaScript 调用任何 Objective-C 的原生接口,替换任意 Objective-C 原生方法。目前主要用于下发 JS 脚本替换原生 Objective-C 代码,实时修复线上 bug。
除了实时修复线上 bug,甚至为 APP 动态添加一个模块也是可行的,不过可能会有性能问题。
使用JSPatch 需要有一个后台可以下发和管理脚本,并且需要处理传输安全等部署工作。
目前有一个JSPatch 平台提供了一系列的服务,只需引入一个 SDK 就能使用 JSPatch,只是还在内测中....
地址 :https://github.com/bang590/JSPatch

CocoPods安装:

Podfile文件

platform :ios, '6.0'
pod 'JSPatch'

然后

pod install

下面开始使用:我们先创建一个列表,再通过JSPath改变行数

1.我们先在Controller里加入一个列表,让他显示3行 ,代码如下:

#import "JRViewController.h"

@interface JRViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,strong)UITableView* myTableView;
@end

@implementation JRViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UITableView* tv =[[UITableView alloc]initWithFrame:self.view.bounds
                                             style:UITableViewStylePlain];
    self.myTableView = tv;
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    [self.view addSubview:self.myTableView];
}
#pragma mark -- UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 3;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString* i=  @"cell";
    UITableViewCell* cell = [tableView  dequeueReusableCellWithIdentifier:i];
    if (cell == nil ) {
        cell =[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault
                                reuseIdentifier:i];
    }
    cell.textLabel.text = @"title";
    return cell;
}

运行一下,显示3行,没问题

Paste_Image.png

2.创建js文件 New File -> ios -> Empty .....

Paste_Image.png

3.在js文件中 写入:

 //将JRViewController类中的- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section方法替换掉
defineClass('JRViewController', {
            tableView_numberOfRowsInSection: function(tableView, section) {
            return 10;
            },
            });

关于JSPath的具体使用,请自行查阅:https://github.com/bang590/JSPatch/wiki

4.调用这个js文件,代码如下:

#import "AppDelegate.h"
#import "JPEngine.h"
@interface AppDelegate ()
@end
@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [JPEngine startEngine];
    NSString* path = [[NSBundle mainBundle]pathForResource:@"JSPathTest" ofType:@"js"];
    NSString* js = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    [JPEngine evaluateScript:js];
    return YES;
}

运行一下,现在是显示10行了 !

Paste_Image.png

**注:
如果我们想要通过 JS 脚本替换原生 Objective-C 代码,实时修复线上 bug的话,还是要通过服务器下发 js脚本 ! **

代码如下:

[NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://........"]]
                                   queue:[NSOperationQueue mainQueue]
                       completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
    NSString *script = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    [JPEngine evaluateScript:script];
}];

工具
将OC代码装换成JSPatch script

相关文章

  • iOS 通过 JSPatch 实时修复线上 bug!

    JSPatch 是一个开源项目(Github链接),只需要在项目里引入极小的引擎文件,就可以使用 JavaScri...

  • JSPatch线上bug实时修复

    JSPatch是什么 JSPatch是一个开源项目,只需要在项目里引入极小的引擎文件,就可以使用 JavaScri...

  • 关于JSPatch使用CGRect踩过的坑

    说道JSPatch,相信iOS开发人员都不陌生,能实时修复iOS bug,想想都让人激动。如果想详细了解请参照大牛...

  • JSPatch进阶

    JSPatch 开源以来大部分被用于 hotfix,替换原生方法修复线上bug,但实际上 JSPatch 一直拥有...

  • iOS之JSPatch 热补丁实时修复bug

    背景 在iOS开发中,存在bug修复周期长的问题。若程序出了bug,往往需要走一下步骤:修改代码--打包--提交审...

  • 使用JSPatch实现实时修复线上bug

    JSPatch简介 JSPatch 是一个开源项目(Github链接),只需要在项目里引入极小的引擎文件,就可以使...

  • JSPatch 热修复技术

    JSPatch 为我们的 iOS 程序提供了一个动态修改代码的能力。比如你的 APP 发生了线上 Bug,通过 J...

  • JSPatch小记

    iOS App发展到今天,越来越多的 App 需要动态部署技术,无需等待周期不定的苹果审核,实时修复线上bug,对...

  • iOS线上修复bug

    以前对于iOS来说,线上出现bug,都很苦恼,因为iOS上线审核周期太长,至少需要一周时间,还是在审核成功的情况下...

  • iOS之JSPatch修改指定控件的文本

    前言:最近用JSPatch修复了线上App一个UILabel文本写错的Bug。其中过程记录下来,希望对大家有帮助。...

网友评论

  • PlusNie:@bang @Ian_He @青楼 @这冬天不会冷 你们现在的应用有用到这个吗,会被拒不?
    X先生_vip:@NiePlus 这个在实际项目中、我还没用!
    PlusNie:@bang @Ian_He @青楼 @这冬天不会冷 不想徒劳无功,有人可分享下结果
    ddaa8dae50b0:之前同事被拒过, 现在没用.

    他被拒可能也因为用这个做了违规的事, 后面不管用它做什么, 用到就是拒
  • Easy_VO:到底会不会被拒啊
  • oriyum:不错
  • 542c8b81e2d4:大量APP一直在正常使用,没有被拒。
  • ddaa8dae50b0:JSPatch 上App Store 很大概率被拒. 同事的这一批都悲剧了.
    ddaa8dae50b0:@黄隆 你可以试试
    IamOnelong:@Ian_He 真的?
    X先生_vip:@Ian_He 这个我还没有试过,如果App Store不允许的话,这个http://jspatch.com平台现在还在内测呢 ,那他们不就悲剧了。。。。

本文标题:iOS 通过 JSPatch 实时修复线上 bug!

本文链接:https://www.haomeiwen.com/subject/kyrdhttx.html