zoukankan      html  css  js  c++  java
  • 热修复

    热修复也是在线修复。由于苹果的审核周期相对较长,遇到重大bug怎么办呢?这时候热修复就显得特别重要。  我也是最近了解到热修复,参考相关资料,来和大家分享一下我所了解到的热修复。

       

       JSPatch,也是今天的主角,这个方案小巧易懂,一个IOS开发者很容易就能上手,它巧妙的运用了runtime的消息转发机制来实现了在线修复,关于消息转发大家可以看看runtime相关的资料,不过值得注意的是JSPatch只能支持ios7以上。想更多了解原理和代码的可以阅读下面的文献:

       

      下面我们直接看看如何使用JSPatch:

      参考的是cocochina社区的一篇文章(http://www.cocoachina.com/ios/20150708/12468.html),因为相比其他大篇幅说JSPatch,这篇文章更简洁。

    1

    2

    3

    4

    5

    6

    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脚本里的实现

     

  • 相关阅读:
    shiro源码篇
    python内置模块之collections(六)
    python之自然语言处理入门(一)
    python第三方库之numpy基础
    python算法之近似熵、互近似熵算法
    MongoDB之conf配置文件详解(五)
    MongoDB之主从复制和副本集(四)
    MongoDB之python简单交互(三)
    python设计模式之常用创建模式总结(二)
    python设计模式之单例模式(一)
  • 原文地址:https://www.cnblogs.com/LiLihongqiang/p/6030378.html
Copyright © 2011-2022 走看看