热修复

热修复也是在线修复。因为苹果的审核周期相对较长,遇到重大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脚本里的实现

相关文章
相关标签/搜索