runtime 实战

2016-11-10  本文已影响0人  maoege

      runtime的确是很神奇的东西,可以做到很多我们本来做不到的事情,系统的UIWebview就有很多很有用的接口,拿到它,我们就能做到跟苹果的Safari一样的体验,下面我就讲一讲利用runtime如何调用私有API

     新建一个Person类 .h 文件如下

    #import  <Foundation/Foundation>

    @interface Person : NSObject

    @end

.m 文件如下

    #import "Person.h"

    @implementation Person

    - (void)eat {

        NSLog(@"eat");

    }

    @end

在其他文件如何调用eat 方法

Person*person = [[Personalloc]init];

    SELtheSEL =nil;

    MyIMPtheIMP =nil;

    [NSObject  test:@"eat"theClass:[personclass]value1:&theSELvalue2:&theIMPvalue3:nil];

    theIMP(person,theSEL);

运行代码神奇的发现了打印 MethodSwizzle[78072:7164695] eat

那么+(BOOL)test:(NSString*)name theClass:(Class)theClass value1:(SEL*)value1 value2:(IMP*)value2 value3:(Method*)value3;这个方法是如何实现的,当然是利用runtime可以动态获取方法列表的特点

    @try{

        unsigned  int  methodCount;

        Method* methods =class_copyMethodList(theClass, &methodCount);

        if(!methods)

        {            returnNO;    }

         for(inti =0; i < methodCount; i++)

        {

            Methodmethod = methods[i];

            p_my_structmethodStruct = (p_my_struct)method;

            constchar* methodName = (constchar*)(sel_getName(methodStruct->name));

            if(methodName)

            {

                const char* theName = [name cStringUsingEncoding:NSUTF8StringEncoding];

                if(strcmp(theName, methodName) ==0)

                {

                    if(value1)

                    {

                        *value1 = methodStruct->name;

                    }

                    if(value2)

                    {

                        *value2 = methodStruct->imp;

                    }

                    if(value3)

                    {

                        *value3 = method;

                    }

                    free(methods);

                    returnYES;

                }

            }

        }

        free(methods);

        returnNO;

    }@catch(NSException *e) {

        NSLog(@"Call %@ failed with error:%@", name, e);

        if(value1)

        {

            *value1 =NULL;

        }

        if(value2)

        {

            *value2 =NULL;

        }

        if(value3)

        {

            *value3 =NULL;

        }

        returnNO;

    }

这段代码的意思就是获取这个类的所有方法,找出这个方法对应的名字,与给出的名字比较,如果相同,返回IMP指针和SEL指针,拿到了这两个东西就可以调用我们的私有方法了,当然如果调用的是系统的私有api,这个api必须先做成加密字符串,用的时候解密即可。

    runtime还可以做到很多事情,方法交换,原理很简单,这里就不细讲,直接给出demo

https://github.com/maoemao/MethodSwizzle

上一篇下一篇

猜你喜欢

热点阅读