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

相關文章
相關標籤/搜索