天天看點

JSPatch: iOS App 動态更新服務平台 什麼是 JSPatch? 什麼是 JSPatch 平台?

iOS App的更新始終是個頭疼的問題,Apple一般的稽核周期需要兩周左右,如果出現嚴重的bug,可以申請下加急稽核,即使這樣,稽核通過的時間也需要好幾個小時,并且加急的次數也是有限的,不能一直使用。幸好現在有一款動态更新的服務平台:JSPatch ,使用它修改bug就得心應手啦。

什麼是 JSPatch?

JSPatch 是一個開源項目(Github連結),隻需要在項目裡引入極小的引擎檔案,就可以使用 JavaScript 調用任何 Objective-C 的原生接口,替換任意 Objective-C 原生方法。目前主要用于下發 JS 腳本替換原生 Objective-C 代碼,實時修複線上 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://test.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
           

http://test.net/bugfix.JS 腳本檔案如下:

//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);
    }
  }
}, {})
           

這樣的話有個麻煩的問題,就是需要每個app自己搭建一個伺服器,用于js腳本檔案的管理。幸好,已經有人幫忙做了這樣的事情

APP動态更新服務平台

什麼是 JSPatch 平台?

JSPatch 需要使用者有一個背景可以下發和管理腳本,并且需要處理傳輸安全等部署工作,JSPatch 平台幫你做了這些事,提供了腳本背景托管,版本管理,保證傳輸安全等功能,讓你無需搭建一個背景,無需關心部署操作,隻需引入一個 SDK 即可立即使用 JSPatch。

在 

AppDelegate.m

 裡載入檔案,并調用 

+startWithAppKey:

 方法,參數為第一步獲得的 AppKey。接着調用 

+sync

 方法檢查更新。例子:

#import <JSPatch/JSPatch.h>
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [JSPatch startWithAppKey:@"你的AppKey"];
    [JSPatch sync];
    ...
}
@end
           

假設已接入 JSPatch SDK 的某線上 APP 發現一處代碼有 bug 導緻 crash:

@implementation XRTableViewController

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
  NSString *content = self.dataSource[[indexPath row]]; //可能會超出數組範圍導緻crash
  XRViewController *controller = [[JPViewController alloc] initWithContent:content];
  [self.navigationController pushViewController:controller];
}

@end
           

上述代碼中取數組元素處可能會超出數組範圍導緻 crash,對此我們寫了如下 JS 腳本準備替換上述方法修複這個 bug:

//main.js
defineClass("XRTableViewController", {
  tableView_didSelectRowAtIndexPath: function(tableView, indexPath) {
    var row = indexPath.row()
    if (self.dataSource().length > row) {  //加上判斷越界的邏輯
      var content = self.dataArr()[row];
      var controller = XRViewController.alloc().initWithContent(content);
      self.navigationController().pushViewController(controller);
    }
  }
})
           

注意在 JSPatch 平台的規範裡,JS腳本的檔案名必須是 

main.js

。接下來就看如何把這個 JS 腳本下發給所有使用者。