iOS热更新-JSPatch实现原理+Patch现场恢复

关于HotfixPatch

在iOS开发领域,由于Apple严格的审核标准和低效率,iOS应用的发版速度极慢,稍微大型的app发版基本上都在一个月以上,所以代码热更新(HotfixPatch)对于iOS应用来说就显得尤其重要。

现在业内基本上都在使用WaxPatch方案,由于Wax框架已经停止维护四五年了,所以waxPatch在使用过程中还是存在不少坑(比如参数转化过程中的问题,如果继承类没有实例化修改继承类的方法无效, wax_gc中对oc中instance的持有延迟释放...)。另外苹果对于Wax使用的态度也处于模糊状态,这也是一个潜在的使用风险。

随着FaceBook开源React Native框架,利用JavaScriptCore.framework直接建立JavaScript(JS)和Objective-C(OC)之间的bridge成为可能,JSPatch也在这个时候应运而生。最开始是从唐巧的微信公众号推送上了解到,开始还以为是在React Native的基础上进行的封装,不过最近仔细研究了源代码,跟React Native半毛钱关系都没有,这里先对JSPatch的作者(不是唐巧,是Bang,博客地址)赞一个。

深入了解JSPatch之后,第一感觉是这个方案小巧,易懂,维护成本低,直接通过OC代码去调用runtime的API,作为一个IOS开发者,很快就能看明白,不用花大精力去了解学习lua。另外在建立JS和OC的Bridge时,作者很巧妙的利用JS和OC两种语言的消息转发机制做了很优雅的实现,稍显不足的是JSPatch只能支持ios7及以上。

由于现在公司的部分应用还在支持ios6,完全取代Wax也不现实,但是一些新上应用已经直接开始支持ios7。个人觉得ios6和ios7的界面风格差别较大,相信应用最低支持版本会很快升级到ios7. 还考虑到JSPatch的成熟度不够,所以决定把JSPatch和WaxPatch结合在一起,相互补充进行使用。下面给大家说一些学习使用体会。

JSPatch和WaxPatch对比

关于JSPatch对比WaxPatch的优势,下面摘抄一下JSPatch作者的话:

方案对比

目前已经有一些方案可以实现动态打补丁,例如WaxPatch,可以用Lua调用OC方法,相对于WaxPatch,JSPatch的优势:

  • 1.JS语言: JS比Lua在应用开发领域有更广泛的应用,目前前端开发和终端开发有融合的趋势,作为扩展的脚本语言,JS是不二之选。

  • 2.符合Apple规则: JSPatch更符合Apple的规则。iOS Developer Program License Agreement里3.3.2提到不可动态下发可执行代码,但通过苹果JavaScriptCore.framework或WebKit执行的代码除外,JS正是通过JavaScriptCore.framework执行的。

  • 3.小巧: 使用系统内置的JavaScriptCore.framework,无需内嵌脚本引擎,体积小巧。

  • 4.支持block: wax在几年前就停止了开发和维护,不支持Objective-C里block跟Lua程序的互传,虽然一些第三方已经实现block,但使用时参数上也有比较多的限制。

JSPatch的劣势:

  • 相对于WaxPatch,JSPatch劣势在于不支持iOS6,因为需要引入JavaScriptCore.framework。另外目前内存的使用上会高于wax,持续改进中。

JSPatch的实现原理理解

JSPatch的实现原理作者的博文已经很详细的介绍了,我这里就不多说了,贴一下学习之处:

看实现原理详解的时候对照着源码看,比较好理解,我在这里说一下我对JSPatch的学习和理解:

(1)OC的动态语言特性

不管是WaxPatch框架还是JSPatch的方案,其根本原理都是利用OC的动态语言特性去动态修改类的方法实现。
OC的动态语言特性是在runtime system(全部用C实现,Apple维护了一份开源代码)上实现的,面向对象的Class和instance机制都是基于消息机制。我们平时认为的[object method],正确的理解应该是[receiver sendMsg], 所有的消息发送会在编译阶段编译为runtime c函数的调用:_obj_sendMsg(id, SEL).

详细介绍参考博文:

runtime提供了一些运行时的API

  • 反射类和选择器
<code class="objectivec">    Class class = <span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSClassFromString</span>(<span class="hljs-string" style="color: rgb(0, 136, 0);">"UIViewController"</span>);
    SEL selector = <span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSSelectorFromString</span>(<span class="hljs-string" style="color: rgb(0, 136, 0);">"viewDidLoad"</span>);</code>
  • 为某个类新增或者替换方法选择器(SEL)的实现(IMP)
<code class="cs">    <span class="hljs-function">BOOL <span class="hljs-title">class_addMethod</span>(<span class="hljs-params" style="color: rgb(102, 0, 102);">Class cls, SEL name, IMP imp, <span class="hljs-keyword" style="color: rgb(0, 0, 136);">const</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136);">char</span> *types</span>)</span>;
    <span class="hljs-function">IMP <span class="hljs-title">class_replaceMethod</span>(<span class="hljs-params" style="color: rgb(102, 0, 102);">Class cls, SEL name, IMP imp, <span class="hljs-keyword" style="color: rgb(0, 0, 136);">const</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136);">char</span> *types</span>)</span>;</code>
  • 在runtime中动态注册类
<code class="lisp">    Class superCls = NSClassFromString<span class="hljs-list">(<span class="hljs-keyword" style="color: rgb(0, 0, 136);">superClassName</span>)</span><span class="hljs-comment" style="color: rgb(136, 0, 0);">;</span>
    cls = objc_allocateClassPair<span class="hljs-list">(<span class="hljs-keyword" style="color: rgb(0, 0, 136);">superCls</span>, className.UTF8String, <span class="hljs-number" style="color: rgb(0, 102, 102);">0</span>)</span><span class="hljs-comment" style="color: rgb(136, 0, 0);">;</span>
    objc_registerClassPair<span class="hljs-list">(<span class="hljs-keyword" style="color: rgb(0, 0, 136);">cls</span>)</span><span class="hljs-comment" style="color: rgb(136, 0, 0);">;</span></code>
(2)JS如何调用OC

在JS运行环境中,需要解决两个问题,一个是OC类对象(objc_class)的获取,另一个就是使用对象提供的接口方法。

对于第一个问题,JSPatch在实现中是通过Require调用在JS环境下创建一个class同名对象(js形式),当向OC发送alloc接收消息之后,会将OC环境中创建的对象地址保存到这个这个js同名对象中,js本身并不完成任何对象的初始化。关于JS持有OC对象的引用,其回收的解释在JSPatch作者的博文中有介绍,没有具体测试。详见JSPatch.js代码:

<code class="scala">    <span class="hljs-comment" style="color: rgb(136, 0, 0);">//请求OC类对象</span>
    <span class="hljs-type">UIView</span> = require(<span class="hljs-string" style="color: rgb(0, 136, 0);">"UIView"</span>);

    <span class="hljs-comment" style="color: rgb(136, 0, 0);">//缓存JS class同名对象</span>
    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">var</span> _require = function(clsName) {
        <span class="hljs-keyword" style="color: rgb(0, 0, 136);">if</span> (!global[clsName]) {
          global[clsName] = {
            __isCls: <span class="hljs-number" style="color: rgb(0, 102, 102);">1</span>,
            __clsName: clsName
          }
        } 
        <span class="hljs-keyword" style="color: rgb(0, 0, 136);">return</span> global[clsName]
      }

    <span class="hljs-comment" style="color: rgb(136, 0, 0);">//调用class方法,返回OC实例化对象进行封装</span>
    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">var</span> ret = instance ? _OC_callI(instance, selectorName, args, isSuper):
                         _OC_callC(clsName, selectorName, args)

    <span class="hljs-comment" style="color: rgb(136, 0, 0);">//OC创建后返回对象</span>
    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">return</span>@{@<span class="hljs-string" style="color: rgb(0, 136, 0);">"__clsName"</span>: <span class="hljs-type">NSStringFromClass</span>([obj <span class="hljs-class"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">class</span>]), <span class="hljs-title" style="color: rgb(102, 0, 102);">@</span>"<span class="hljs-title" style="color: rgb(102, 0, 102);">__obj</span>":</span> obj};


    <span class="hljs-comment" style="color: rgb(136, 0, 0);">//JS中解析OC对象</span>
    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">return</span> _formatOCToJS(ret)

    <span class="hljs-comment" style="color: rgb(136, 0, 0);">//_formatOCToJS</span>
    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">if</span> (obj instanceof <span class="hljs-type">Object</span>) {
        <span class="hljs-keyword" style="color: rgb(0, 0, 136);">var</span> ret = {}
        <span class="hljs-keyword" style="color: rgb(0, 0, 136);">for</span> (<span class="hljs-keyword" style="color: rgb(0, 0, 136);">var</span> key in obj) {
          ret[key] = _formatOCToJS(obj[key])
        }
        <span class="hljs-keyword" style="color: rgb(0, 0, 136);">return</span> ret
     }</code>

对于第二个问题,JSPatch在JS环境中通过中心转发方式,所有OC方法的调用均是通过新增Object(js)原型方法_c(methodName)完成调用,在通过JavaScriptCore执行JS脚本之前,先将所有的方法调用字符替换
_c('method')的方式; 在_c函数中通过JSContex建立的桥接函数传入参数和返回参数即完成了调用;

<code class="objectivec">    <span class="hljs-comment" style="color: rgb(136, 0, 0);">//字符替换</span>
    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">static</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSString</span> *_regexStr = <span class="hljs-string" style="color: rgb(0, 136, 0);">@"\\.\\s*(\\w+)\\s*\\("</span>;
    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">static</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSString</span> *_replaceStr = <span class="hljs-string" style="color: rgb(0, 136, 0);">@".__c(\"$1\")("</span>;

    <span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSString</span> *formatedScript = [<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSString</span> stringWithFormat:<span class="hljs-string" style="color: rgb(0, 136, 0);">@"try{@}catch(e){_OC_catch(e.message, e.stack)}"</span>, [_regex stringByReplacingMatchesInString:script options:<span class="hljs-number" style="color: rgb(0, 102, 102);">0</span> range:<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSMakeRange</span>(<span class="hljs-number" style="color: rgb(0, 102, 102);">0</span>, script<span class="hljs-variable" style="color: rgb(102, 0, 102);">.length</span>) withTemplate:_replaceStr]];


    <span class="hljs-comment" style="color: rgb(136, 0, 0);">//__c()向OC转发调用参数</span>
    Object<span class="hljs-variable" style="color: rgb(102, 0, 102);">.prototype</span><span class="hljs-variable" style="color: rgb(102, 0, 102);">.__c</span> = function(methodName) {

        ...

        <span class="hljs-keyword" style="color: rgb(0, 0, 136);">return</span> function(){
          var args = Array<span class="hljs-variable" style="color: rgb(102, 0, 102);">.prototype</span><span class="hljs-variable" style="color: rgb(102, 0, 102);">.slice</span><span class="hljs-variable" style="color: rgb(102, 0, 102);">.call</span>(arguments)
          <span class="hljs-keyword" style="color: rgb(0, 0, 136);">return</span> _methodFunc(<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102);">.__obj</span>, <span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102);">.__clsName</span>, methodName, args, <span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102);">.__isSuper</span>)
        }
     }

    <span class="hljs-comment" style="color: rgb(136, 0, 0);">//_methodFunc调用桥接函数</span>
    var _methodFunc = function(instance, clsName, methodName, args, isSuper) {

        ...

        var ret = instance ? _OC_callI(instance, selectorName, args, isSuper):
                             _OC_callC(clsName, selectorName, args)

        <span class="hljs-keyword" style="color: rgb(0, 0, 136);">return</span> _formatOCToJS(ret)
     }


    <span class="hljs-comment" style="color: rgb(136, 0, 0);">//OC中的桥接函数,JS和OC的桥接函数都是通过这样定义</span>
    context[<span class="hljs-string" style="color: rgb(0, 136, 0);">@"_OC_callI"</span>] = ^<span class="hljs-keyword" style="color: rgb(0, 0, 136);">id</span>(JSValue *obj, <span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSString</span> *selectorName, JSValue *arguments, <span class="hljs-built_in" style="color: rgb(102, 0, 102);">BOOL</span> isSuper) {
        <span class="hljs-keyword" style="color: rgb(0, 0, 136);">return</span> callSelector(<span class="hljs-literal" style="color: rgb(0, 102, 102);">nil</span>, selectorName, arguments, obj, isSuper);
    };

    context[<span class="hljs-string" style="color: rgb(0, 136, 0);">@"_OC_callC"</span>] = ^<span class="hljs-keyword" style="color: rgb(0, 0, 136);">id</span>(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSString</span> *className, <span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSString</span> *selectorName, JSValue *arguments) {
        <span class="hljs-keyword" style="color: rgb(0, 0, 136);">return</span> callSelector(className, selectorName, arguments, <span class="hljs-literal" style="color: rgb(0, 102, 102);">nil</span>, <span class="hljs-literal" style="color: rgb(0, 102, 102);">NO</span>);
    };</code>
(3)JS如何替换OC方法

JSPatch的主要作用还是通过脚本修复一些线上bug,希望能够达到替换OC方法的目标。JSPatch的实现巧妙之处在于:利用了OC的消息转发机制

  • 1:替换原有selector的IMP实现为一个空的IMP实现,这样当objc_class接受到消息之后,就会进行消息转发, 另外需要将selector的初始实现进行保存;
<code class="objectivec">    <span class="hljs-comment" style="color: rgb(136, 0, 0);">//selector指向空实现</span>
    IMP msgForwardIMP = getEmptyMsgForwardIMP(typeDescription, methodSignature);
    class_replaceMethod(cls, selector, msgForwardIMP, typeDescription);


    <span class="hljs-comment" style="color: rgb(136, 0, 0);">//保存原有实现,这里进行了修改,增加了恢复现场的支持</span>
    <span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSString</span> *originalSelectorName = [<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSString</span> stringWithFormat:<span class="hljs-string" style="color: rgb(0, 136, 0);">@"ORIG@"</span>, selectorName];
    SEL originalSelector = <span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSSelectorFromString</span>(originalSelectorName);
    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">if</span>(class_respondsToSelector(cls, selector)) {
        <span class="hljs-keyword" style="color: rgb(0, 0, 136);">if</span>(!class_respondsToSelector(cls, originalSelector)){
            class_addMethod(cls, originalSelector, originalImp, typeDescription);
        } <span class="hljs-keyword" style="color: rgb(0, 0, 136);">else</span> {
            class_replaceMethod(cls, originalSelector, originalImp, typeDescription);
        }
    }</code>
  • 2:将替换的JS方法构造一个JPSelector及其IMP实现(根据返回参数构造),添加到当前class中,并通过cls+selecotr全局缓存JS方法(全局缓存并没有多大用途,但是对于后面恢复现场比较有用);
<code class="php">    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">if</span> (!_JSOverideMethods[clsName][JPSelectorName]) {
        _initJPOverideMethods(clsName);
        _JSOverideMethods[clsName][JPSelectorName] = <span class="hljs-function"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">function</span></span>;
        <span class="hljs-keyword" style="color: rgb(0, 0, 136);">const</span> char *returnType = [methodSignature methodReturnType];
        IMP JPImplementation = <span class="hljs-keyword" style="color: rgb(0, 0, 136);">NULL</span>;

        <span class="hljs-comment" style="color: rgb(136, 0, 0);">//根据返回类型构造</span>
        <span class="hljs-keyword" style="color: rgb(0, 0, 136);">switch</span> (returnType[<span class="hljs-number" style="color: rgb(0, 102, 102);">0</span>]){
         ...
        }

        <span class="hljs-keyword" style="color: rgb(0, 0, 136);">if</span>(!class_respondsToSelector(cls, JPSelector)){
            class_addMethod(cls, JPSelector, JPImplementation, typeDescription);
        } <span class="hljs-keyword" style="color: rgb(0, 0, 136);">else</span> {
            class_replaceMethod(cls, JPSelector, JPImplementation,typeDescription);
        }
    }</code>
  • 3:然后改写每个替换方法类的forwadInvocation的实现进行拦截,如果拦截到的Invocation的selctor转化成JPSelector能够响应,说明是一个替换方法,则从Invocation中取参数后调用JPSelector的IMP;
<code class="objectivec">    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">static</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136);">void</span> JPForwardInvocation(<span class="hljs-keyword" style="color: rgb(0, 0, 136);">id</span> slf, SEL selector, <span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSInvocation</span> *invocation)
    {
        <span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSMethodSignature</span> *methodSignature = [invocation methodSignature];
        <span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSInteger</span> numberOfArguments = [methodSignature numberOfArguments];

        <span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSString</span> *selectorName = <span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSStringFromSelector</span>(invocation<span class="hljs-variable" style="color: rgb(102, 0, 102);">.selector</span>);
        <span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSString</span> *JPSelectorName = [<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSString</span> stringWithFormat:<span class="hljs-string" style="color: rgb(0, 136, 0);">@"_JP@"</span>, selectorName];
        SEL JPSelector = <span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSSelectorFromString</span>(JPSelectorName);

        <span class="hljs-keyword" style="color: rgb(0, 0, 136);">if</span> (!class_respondsToSelector(object_getClass(slf), JPSelector)) {
            ...
        }

        <span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSMutableArray</span> *argList = [[<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSMutableArray</span> alloc] init];
        [argList addObject:slf];

        <span class="hljs-keyword" style="color: rgb(0, 0, 136);">for</span> (<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSUInteger</span> i = <span class="hljs-number" style="color: rgb(0, 102, 102);">2</span>; i < numberOfArguments; i++) {
            ...
        }

        <span class="hljs-comment" style="color: rgb(136, 0, 0);">//获取参数之后invoke JPSector调用JSFunction的实现</span>
        <span class="hljs-keyword" style="color: rgb(0, 0, 136);">@synchronized</span>(_context) {
            _T<span class="hljs-built_in" style="color: rgb(102, 0, 102);">MPInvocationArguments</span> = formatOCToJSList(argList);

            [invocation setSelector:JPSelector];
            [invocation invoke];

            _T<span class="hljs-built_in" style="color: rgb(102, 0, 102);">MPInvocationArguments</span> = <span class="hljs-literal" style="color: rgb(0, 102, 102);">nil</span>;
        }
    }</code>

Patch现场复原的补充

Patch现场恢复的功能主要用于连续更新脚本的应用场景。由于IOS的App应用按Home键或者被电话中断的时候,应用实际上是首先进入到后台运行阶段(applicationWillResignActive),当我们下次再次使用App的时候,如果后台应用没有被终止(applicationWillTerminate),那么App不会走appliation:didFinishLaunchingWithOptions方法,而是会走(applicationWillEnterForeground)。 对于这种场景如果我们连续更新线上脚本,那么第二次脚本更新则无法保留最开始的方法实现,另外恢复现场功能也有助于我们撤销线上脚本能够恢复应用的本身代码功能。

JSPatch的现场恢复

本文在JSPatch基础上添加了现场恢复功能;源码地址参考:

说明如下:

(1)在JPEngine.h 中添加了两个启动和结束的调用函数如下:

<code class="cs">    <span class="hljs-function"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">void</span> <span class="hljs-title">js_start</span>(<span class="hljs-params" style="color: rgb(102, 0, 102);">NSString* initScript</span>)</span>;
    <span class="hljs-function"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">void</span> <span class="hljs-title">js_end</span>(<span class="hljs-params" style="color: rgb(102, 0, 102);"></span>)</span>;</code>

(2) JPEngine.m 中调用函数的实现以及恢复现场对部分代码的修改:主要是利用了替换方法和新增方法的cache(_JSOverideMethods, 主要是这个)

<code class="objectivec">    <span class="hljs-comment" style="color: rgb(136, 0, 0);">//处理替换方法,selector指回最初的IMP,JPSelector和ORIGSelector都指向未实现IMP</span>
     <span class="hljs-keyword" style="color: rgb(0, 0, 136);">if</span>([JPSelectorName hasPrefix:<span class="hljs-string" style="color: rgb(0, 136, 0);">@"_JP"</span>]){
         <span class="hljs-keyword" style="color: rgb(0, 0, 136);">if</span> (class_getMethodImplementation(cls, <span class="hljs-keyword" style="color: rgb(0, 0, 136);">@selector</span>(forwardInvocation:)) == (IMP)JPForwardInvocation) {
             SEL ORIGforwardSelector = <span class="hljs-keyword" style="color: rgb(0, 0, 136);">@selector</span>(ORIGforwardInvocation:);
             IMP ORIGforwardImp = class_getMethodImplementation(cls, ORIGforwardSelector);
             class_replaceMethod(cls, <span class="hljs-keyword" style="color: rgb(0, 0, 136);">@selector</span>(forwardInvocation:), ORIGforwardImp, <span class="hljs-string" style="color: rgb(0, 136, 0);">"v@:@"</span>);
             class_replaceMethod(cls, ORIGforwardSelector, _objc_msgForward, <span class="hljs-string" style="color: rgb(0, 136, 0);">"v@:@"</span>);
         }


         <span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSString</span> *selectorName = [JPSelectorName stringByReplacingOccurrencesOfString:<span class="hljs-string" style="color: rgb(0, 136, 0);">@"_JP"</span> withString:<span class="hljs-string" style="color: rgb(0, 136, 0);">@""</span>];
         <span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSString</span> *ORIGSelectorName = [JPSelectorName stringByReplacingOccurrencesOfString:<span class="hljs-string" style="color: rgb(0, 136, 0);">@"_JP"</span> withString:<span class="hljs-string" style="color: rgb(0, 136, 0);">@"ORIG"</span>];

         SEL JPSelector = <span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSSelectorFromString</span>(JPSelectorName);
         SEL selector = <span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSSelectorFromString</span>(selectorName);
         SEL ORIGSelector = <span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSSelectorFromString</span>(ORIGSelectorName);

         <span class="hljs-keyword" style="color: rgb(0, 0, 136);">if</span>(class_respondsToSelector(cls, ORIGSelector) &&
            class_respondsToSelector(cls, selector) &&
            class_respondsToSelector(cls, JPSelector)){
             <span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSMethodSignature</span> *methodSignature = [cls instanceMethodSignatureForSelector:ORIGSelector];
             Method method = class_getInstanceMethod(cls, ORIGSelector);
             <span class="hljs-keyword" style="color: rgb(0, 0, 136);">char</span> *typeDescription = (<span class="hljs-keyword" style="color: rgb(0, 0, 136);">char</span> *)method_getTypeEncoding(method);
             IMP forwardEmptyIMP = getEmptyMsgForwardIMP(typeDescription, methodSignature);
             IMP ORIGSelectorImp = class_getMethodImplementation(cls, ORIGSelector);

             class_replaceMethod(cls, selector, ORIGSelectorImp, typeDescription);
             class_replaceMethod(cls, JPSelector, forwardEmptyIMP, typeDescription);
             class_replaceMethod(cls, ORIGSelector, forwardEmptyIMP, typeDescription);
         }
     }

     <span class="hljs-comment" style="color: rgb(136, 0, 0);">//处理添加的新方法</span>
     <span class="hljs-keyword" style="color: rgb(0, 0, 136);">else</span> {
         isClsNew = <span class="hljs-literal" style="color: rgb(0, 102, 102);">YES</span>;
         SEL JPSelector = <span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSSelectorFromString</span>(JPSelectorName);
         <span class="hljs-keyword" style="color: rgb(0, 0, 136);">if</span>(class_respondsToSelector(cls, JPSelector)){
             <span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSMethodSignature</span> *methodSignature = [cls instanceMethodSignatureForSelector:JPSelector];
             Method method = class_getInstanceMethod(cls, JPSelector);
             <span class="hljs-keyword" style="color: rgb(0, 0, 136);">char</span> *typeDescription = (<span class="hljs-keyword" style="color: rgb(0, 0, 136);">char</span> *)method_getTypeEncoding(method);
             IMP forwardEmptyIMP = getEmptyMsgForwardIMP(typeDescription, methodSignature);

             class_replaceMethod(cls, JPSelector, forwardEmptyIMP, typeDescription);
         }
     }</code>

HotfixPatch的那些坑

JSPatch在使用过程中也会遇到不少坑,虽然这两个框架现在虽然都能够做到新增可执行代码,但是将其应用到开发功能组件还不太可取。比如说我在第一次使用JSPatch遇到了一个坑:

  • 在JS脚本改写派生类中未实现的继承类的 optional protocol方法时,tableView reload的时候不会调用JS的补丁方法,但是在tableView中显式调用可以调用替换的selector方法;另外如果在派生类中重写这个protocol方法,则可以调起;

  • 转:http://blog.csdn.net/antsuperman/article/details/51176639

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值