JSPatch动态修改已上架app的bug,即时修复bug

JSPatch 是最近业余做的小项目,只需在项目中引入极小的引擎,就可以使用JavaScript调用任何Objective-C的原生接口,获得脚本语言的能力:动态更新APP,替换项目原生代码修复bug。

用途

是否有过这样的经历:新版本上线后发现有个严重的bug,可能会导致crash率激增,可能会使网络请求无法发出,这时能做的只是赶紧修复bug然后提交等待漫长的AppStore审核,再盼望用户快点升级,付出巨大的人力和时间成本,才能完成此次bug的修复。

使用JSPatch可以解决这样的问题,只需在项目中引入JSPatch,就可以在发现bug时下发JS脚本补丁,替换原生方法,无需更新APP即时修复bug。

例子

@implementation JPTableViewController ... - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {   NSString *content = self.dataSource[[indexPath row]];  //可能会超出数组范围导致crash   JPViewController *ctrl = [[JPViewController alloc] initWithContent:content];   [self.navigationController pushViewController:ctrl]; } ... @end

上述代码中取数组元素处可能会超出数组范围导致crash。如果在项目里引用了JSPatch,就可以下发JS脚本修复这个bug:

#import “JPEngine.m" @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {  [JPEngine startEngine];  [NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://cnbang.net/bugfix.JS"]] queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {  NSString *script = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];  if (script) {    [JPEngine evaluateScript:script];  } }];    ….  return YES; } @end 
//JS defineClass("JPTableViewController", {   //instance method definitions   tableView_didSelectRowAtIndexPath: function(tableView, indexPath) {     var row = indexPath.row()     if (self.dataSource().length > row) {  //加上判断越界的逻辑       var content = self.dataArr()[row];       var ctrl = JPViewController.alloc().initWithContent(content);       self.navigationController().pushViewController(ctrl);     }   } }, {})

这样 JPTableViewController 里的 -tableView:didSelectRowAtIndexPath: 就替换成了这个JS脚本里的实现,在用户无感知的情况下修复了这个bug。

原文地址:https://www.cnblogs.com/dbaiyunyun/p/4974636.html