使用JSPatch平臺熱修復iosApp

IOS產品開發中經常會遇到這種狀況,線上發現一個嚴重bug,多是一個crash,多是一個功能沒法使用,這時能作的只是趕忙修復Bug而後提交等待漫長的審覈,即便申請加急也不會快到那裏去,即便審覈完了以後,還要盼望着用戶快點升級,用戶不升級仍是在存在一樣的漏洞,這樣的狀況讓開發者付出了很大的成本才能完成Bug的修復。javascript

JSPath就是爲了解決這樣的問題而出現的,只須要在項目中引入極小的JSPatch引擎,就能夠還用JavaScript語言調用Objective-C的原生API,動態更新APP,修復BUG。java

JSPaht自己是開源項目,項目地址:http://jspatch.com/,github地址: https://github.com/bang590/JSPatch
在他的官網上面給出了一個例子:ios

 1 @implementation JPTableViewController
 2 ...
 3 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
 4 {
 5   NSString *content = self.dataSource[[indexPath row]];  //可能會超出數組範圍致使crash
 6   JPViewController *ctrl = [[JPViewController alloc] initWithContent:content];
 7   [self.navigationController pushViewController:ctrl];
 8 }
 9 ...
10 @end

能夠經過下發下面的JavaScript代碼修復這個bug:git

 1 //JS
 2 defineClass("JPTableViewController", {
 3   //instance method definitions
 4   tableView_didSelectRowAtIndexPath: function(tableView, indexPath) {
 5     var row = indexPath.row()
 6     if (self.dataSource().length > row) {  //加上判斷越界的邏輯
 7       var content = self.dataArr()[row];
 8       var ctrl = JPViewController.alloc().initWithContent(content);
 9       self.navigationController().pushViewController(ctrl);
10     }
11   }
12 }, {})

JSPtch須要一個後臺服務用來下發和管理腳本,並須要處理傳輸安全等。github

註冊獲取AppKey

在平臺上面註冊一個帳戶,新建一個App能夠拿到對應的AppKey。數組

導入SDK到項目中

SDK地址:http://jspatch.com/Index/sdk
當前下載下來的SDK版本名稱是:JSPatch 2.framework,須要去掉中間的空格,否則導入項目的時候會報錯。
導入項目的時候要選擇Copy items if needed
還須要添加對於的依賴框架JavaScriptCore.frameworklibz.tbd.安全

添加JSPatch代碼

在AppDelegate.m中添加代碼:服務器

 1 #import "AppDelegate.h"
 2 #import <JSPatch/JSPatch.h>
 3 
 4 @implementation AppDelegate
 5 
 6 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
 7     [JSPatch startWithAppKey:@"f78378d77e5783e8"];
 8     [JSPatch sync];
 9     return YES;
10 }
11 @end

在平臺中上傳js修復文件

爲了簡單咱們只上傳一個簡單的UIAlertView,彈出一個提示框:app

1 ar alertView = require('UIAlertView').alloc().init();
2 alertView.setTitle('Alert');
3 alertView.setMessage('AlertView from js');
4 alertView.addButtonWithTitle('OK');
5 alertView.show();

用JavaScript實例化了UIAlertView,文件名須要命名爲main.js框架

從服務器下發到客戶端

main.js上傳到服務器上,下發到版本爲1.0的客戶端上面。
在請求服務加載腳本的時候出現了一個錯誤:The resource could not be loaded because the App Transport Security policy requires the use of a secure connection.
這個錯誤出現的緣由是ios9引入了新特性App Transport Security(ATS),簡單來講就是APP內部的請求必須使用HTTPS協議。
很明顯這裏的url並無使用https,咱們能夠經過設置先規避掉這個問題:

1.在info.plist中添加NSAppTransportSecurity類型爲Dictionary. 2.在NSAppTransportSecurity中添加NSAllowsArbitraryLoads類型爲Boolean,值爲YES

運行效果:

相關文章
相關標籤/搜索