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

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

用途

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

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

例子

@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:post

#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。ui

相关文章
相关标签/搜索