贴点国外大神代码,没事瞅瞅

简介: // 运行时,这整的 void SwizzleClassMethod(Class c,SEL orig, SEL new) {     Method origMethod =class_getClassMethod(c, orig);     Method newMethod =class_getClassMethod(c, new);          c = ob


// 运行时,这整的

void SwizzleClassMethod(Class c,SEL orig, SEL new)

{

    Method origMethod =class_getClassMethod(c, orig);

    Method newMethod =class_getClassMethod(c, new);

    

    c = object_getClass((id)c);

    

    if(class_addMethod(c, orig,method_getImplementation(newMethod),method_getTypeEncoding(newMethod)))

        class_replaceMethod(c, new,method_getImplementation(origMethod),method_getTypeEncoding(origMethod));

    else

        method_exchangeImplementations(origMethod, newMethod);

}


void SwizzleInstanceMethod(Class c,SEL orig, SEL new)

{

    Method origMethod =nil, newMethod = nil;

    

    origMethod = class_getInstanceMethod(c, orig);

    newMethod = class_getInstanceMethod(c, new);

    if ((origMethod !=nil) && (newMethod != nil))

    {

        if(class_addMethod(c, orig,method_getImplementation(newMethod),method_getTypeEncoding(newMethod)))

            class_replaceMethod(c, new,method_getImplementation(origMethod),method_getTypeEncoding(origMethod));

        else

            method_exchangeImplementations(origMethod, newMethod);

    }

    else

        NSLog(@"Attempt to swizzle nonexistent methods!");

}


void SwizzleInstanceMethodWithAnotherClass(Class c1,SEL orig, Class c2, SEL new)

{

    Method origMethod =nil, newMethod = nil;

    

    origMethod = class_getInstanceMethod(c1, orig);

    newMethod = class_getInstanceMethod(c2, new);

    if ((origMethod !=nil) && (newMethod != nil))

    {

        if(class_addMethod(c1, orig,method_getImplementation(newMethod),method_getTypeEncoding(newMethod)))

            class_replaceMethod(c1, new,method_getImplementation(origMethod),method_getTypeEncoding(origMethod));

        else

            method_exchangeImplementations(origMethod, newMethod);

    }

    else

        NSLog(@"Attempt to swizzle nonexistent methods!");

}


void InjectClassMethodFromAnotherClass(Class toClass, Class fromClass,SEL fromSelector, SEL toSeletor)

{

    Method method =class_getClassMethod(fromClass, fromSelector);

    if (method !=nil)

    {

        if (!class_addMethod(toClass, toSeletor,method_getImplementation(method),method_getTypeEncoding(method)))

            NSLog(@"Attempt to add method failed");

    }

    else

        NSLog(@"Attempt to add nonexistent method");

}


void InjectInstanceMethodFromAnotherClass(Class toClass, Class fromClass,SEL fromSelector, SEL toSeletor)

{

    Method method =class_getInstanceMethod(fromClass, fromSelector);

    if (method !=nil)

    {

        if (!class_addMethod(toClass, toSeletor,method_getImplementation(method),method_getTypeEncoding(method)))

            NSLog(@"Attempt to add method failed");

    }

    else

        NSLog(@"Attempt to add nonexistent method");

}





// 牛X 的方法,谁来弄弄


+ (void)setApplicationStatusBarAlpha:(float)alpha

{

    staticSEL selector = NULL;

    if (selector ==NULL)

    {

        NSString *str1 =@"rs`str";

        NSString *str2 =@"A`qVhmcnv";

        

        selector = NSSelectorFromString([[NSStringalloc] initWithFormat:@"%@%@",TGEncodeText(str1, 1),TGEncodeText(str2, 1)]);

    }

    

    if ([[UIApplicationsharedApplication] respondsToSelector:selector])

    {

#pragma clang diagnostic push

#pragma clang diagnostic ignored "-Warc-performSelector-leaks"

        UIWindow *window = [[UIApplicationsharedApplication] performSelector:selector];

#pragma clang diagnostic pop

        

        window.alpha = alpha;

    }

}


static UIView *findStatusBarView()

{

    static Class viewClass =nil;

    staticSEL selector = NULL;

    if (selector ==NULL)

    {

        NSString *str1 =@"rs`str";

        NSString *str2 =@"A`qVhmcnv";

        

        selector = NSSelectorFromString([[NSStringalloc] initWithFormat:@"%@%@",TGEncodeText(str1, 1),TGEncodeText(str2, 1)]);

        

        viewClass = NSClassFromString(TGEncodeText(@"VJTubuvtCbs", -1));

    }

    

    if ([[UIApplicationsharedApplication] respondsToSelector:selector])

    {

#pragma clang diagnostic push

#pragma clang diagnostic ignored "-Warc-performSelector-leaks"

        UIWindow *window = [[UIApplicationsharedApplication] performSelector:selector];

#pragma clang diagnostic pop

        

        for (UIView *subviewin window.subviews)

        {

            if ([subviewisKindOfClass:viewClass])

            {

                return subview;

            }

        }

    }

    

    returnnil;

}





//补充上面的方法

NSString *TGEncodeText(NSString *string,int key)

{

    NSMutableString *result = [[NSMutableStringalloc] init];

    

    for (int i =0; i < (int)[stringlength]; i++)

    {

        unichar c = [stringcharacterAtIndex:i];

        c += key;

        [result appendString:[NSStringstringWithCharacters:&c length:1]];

    }

    

    return result;

}


NSString *TGStringMD5(NSString *string)

{

    constchar *ptr = [string UTF8String];

    unsignedchar md5Buffer[16];

    CC_MD5(ptr, (CC_LONG)[stringlengthOfBytesUsingEncoding:NSUTF8StringEncoding], md5Buffer);

    NSString *output = [[NSStringalloc] initWithFormat:@"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x", md5Buffer[0], md5Buffer[1], md5Buffer[2], md5Buffer[3], md5Buffer[4], md5Buffer[5], md5Buffer[6], md5Buffer[7], md5Buffer[8], md5Buffer[9], md5Buffer[10], md5Buffer[11], md5Buffer[12], md5Buffer[13], md5Buffer[14], md5Buffer[15]];


    return output;

}










目录
相关文章
|
5月前
|
Java 容器
膜拜!清华大佬手撸多线程并发源码笔记Github上线3天星标35k+
你为什么要学习多线程?是因为理想吗?是因为热爱吗? 哦~原来是为了面试打基础、做准备啊!没错,这真的很现实!
膜拜!清华大佬手撸多线程并发源码笔记Github上线3天星标35k+
|
9月前
|
搜索推荐 JavaScript 前端开发
如何写一个程序,哄女朋友或者老婆开心?
@[TOC](目录) 如何写一个程序,哄女朋友开心?下面有个列子 要编写一个让女朋友高兴的程序,需要考虑以下几点: 1. 了解女朋友的兴趣爱好:如果女朋友喜欢音乐,可以在程序中添加音乐播放功能;如果女朋友喜欢拍照,可以在程序中添加拍照和修图功能。 2. 考虑女朋友的需求:如果女朋友需要管理日历和任务,可以在程序中添加这些功能,帮助她更好地管理时间和任务。 3. 添加个性化元素:在程序中添加一些女朋友喜欢的元素,比如她的名字、照片、喜欢的颜色等等,让程序更加个性化和特别。 4. 添加互动元素:在程序中添加一些互动元素,比如发送生日祝福、情人节祝福等等,让女朋友感受到自己的关心和爱。 下面是一些可
142 0
|
JavaScript 前端开发
不看后悔系列!原来代码还可以这么写!
不看后悔系列!原来代码还可以这么写!
|
SQL 存储 监控
聊聊那些年遇到过的奇葩代码
无论是开发新需求还是维护旧平台,在工作的过程中我们都会接触到各种样式的代码,有时候会碰到一些优秀的代码心中不免肃然起敬,但是更多的时候我们会遇到很多奇葩代码,有的时候骂骂咧咧的吐槽一段奇葩代码后定睛一看作者,居然是几个月以前自己的写的,心中难免浮现曹操的那句名言:不可能,绝对不可能。
聊聊那些年遇到过的奇葩代码
|
前端开发 JavaScript 容器
【一个让你停不下来的动效】——难道不来瞅瞅?(含源码+思路)
【一个让你停不下来的动效】——难道不来瞅瞅?(含源码+思路)
130 0
【一个让你停不下来的动效】——难道不来瞅瞅?(含源码+思路)
|
JavaScript 前端开发
面试官:听说过LazyMan吗,手写一个试试
我们都知道,JavaScript是单线程、基于异步的编程语言,执行代码时是非阻塞的。如果执行的是同步的代码,则按照顺序执行完毕;如果是异步的代码,则按照异步调用执行,并不会阻塞在某一个异步函数中。最近遇到了这样一个面试题……
255 0
面试官:听说过LazyMan吗,手写一个试试
|
Java Linux 程序员
撒花!cxuan 硬肝的 GitHub 不来瞅瞅?(一)
之前一直有很多小伙伴们找我,让我聊一聊如何学习 Java ,我都直接回复了一个思维导图,后来想一想觉得回答不是很认真,我的初衷是想让小伙伴们根据思维导图中的知识点,采取各个击破 的原则,哪里不会查哪里,后来想想这种回答方式没有多少人能够直接接受。大家更想要的是从我这里获得点什么。
撒花!cxuan 硬肝的 GitHub 不来瞅瞅?(一)
|
消息中间件 Java 关系型数据库
撒花!cxuan 硬肝的 GitHub 不来瞅瞅?(二)
之前一直有很多小伙伴们找我,让我聊一聊如何学习 Java ,我都直接回复了一个思维导图,后来想一想觉得回答不是很认真,我的初衷是想让小伙伴们根据思维导图中的知识点,采取各个击破 的原则,哪里不会查哪里,后来想想这种回答方式没有多少人能够直接接受。大家更想要的是从我这里获得点什么。
撒花!cxuan 硬肝的 GitHub 不来瞅瞅?(二)
|
算法
奇葩面试题,O(logn)的底数是多少?
奇葩面试题,O(logn)的底数是多少?
185 0
奇葩面试题,O(logn)的底数是多少?
|
算法 Java
别在网上乱找代码了,找了一段代码突然爆了!!!
本人是做游戏服务器开发的,碰到一个需求,给符某些要求的玩家的发送道具奖励,奖励的数量根据离线的天数计算。 这个需求实现起来很简单,只需要在玩家上线的时候计算上次离线时间和当前时间间隔的天数,然后根据策划的算法,计算出道具种类与数量,发一封邮件给玩家就可以了。