熱修復

熱修復也是在線修復。因爲蘋果的審覈週期相對較長,遇到重大bug怎麼辦呢?這時候熱修復就顯得特別重要。  我也是最近了解到熱修復,參考相關資料,來和你們分享一下我所瞭解到的熱修復。html

   

   JSPatch,也是今天的主角,這個方案小巧易懂,一個IOS開發者很容易就能上手,它巧妙的運用了runtime的消息轉發機制來實現了在線修復,關於消息轉發你們能夠看看runtime相關的資料,不過值得注意的是JSPatch只能支持ios7以上。想更多瞭解原理和代碼的能夠閱讀下面的文獻:ios

   

  下面咱們直接看看如何使用JSPatch:git

  參考的是cocochina社區的一篇文章(http://www.cocoachina.com/ios/20150708/12468.html),由於相比其餘大篇幅說JSPatch,這篇文章更簡潔。github

1數組

2app

3lua

4spa

5.net

6htm

7

8

9

10

@implementation JPTableViewContro

ller

...

- (void)tableView:(UITableView *)

tableView didSelectRowAtIndex

Path:(NSIndexPath *)indexPath

{

  NSString *content = self.dataSource[[indexPath row]];  

//可能會超出數組範圍致使crash

  JPViewController *ctrl = 

[[JPViewController alloc] initWith

Content:content];

  [self.navigationController 

pushViewController:ctrl];

}

...

@end

上述代碼中取數組元素處可能會超出數組範圍致使crash。

第一步:導入JSPatch的相關文件

第二步:

#import 「JPEngine.m"

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application 

didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

    [JPEngine startEngine];

    [NSURLConnection sendAsynchronous

Request:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://cnbang.net/bugfix.JS"]] queue:[NSOperationQueue mainQueue] completion

Handler:^(NSURLResponse *response, 

NSData *data, NSError *connectionError) {

    NSString *script = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

    if (script) {

      [JPEngine evaluateScript:script];

    }

}];

   ….

    return YES;

}

@end

 

第三步:按照Demo的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);

    }

  }

}, {})

這樣 JPTableViewController 裏的 -tableView:didSelectRowAtIndexPath: 就替換成了這個JS腳本里的實現

相關文章
相關標籤/搜索