runtime 实战

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

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • runtime的确是很神奇的东西,可以做到很多我们本来做不到的事情,系统的UIWebview就有很多很有用的...
    maoege阅读 1,724评论 0 0
  • 转自其他博客 现在IT届流行一个词叫“黑科技”,泛指一些让人“不明觉厉的”新技术或新产品,那在iOS开发中有什么高...
    BBH_Life阅读 3,122评论 0 0
  • 转至元数据结尾创建: 董潇伟,最新修改于: 十二月 23, 2016 转至元数据起始第一章:isa和Class一....
    40c0490e5268阅读 5,890评论 0 9
  • 这是OC运行时实战应用系列的第二篇,你可以在这里找到实战应用1,这一片主要从消息发送,消息转发,消息交换的角度讲解...
    CerasusLand阅读 1,274评论 0 0
  • PS.赞扬的都写在前面… 欢迎你来到我的梦里。 梦,这只是一场梦。谁的梦?是椿?是梁旋?还是观影的你我? 12年前...
    不可执迷阅读 3,935评论 0 1

友情链接更多精彩内容