贴点国外大神代码,没事瞅瞅-阿里云开发者社区

开发者社区> 云计算> 正文

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

简介: // 运行时,这整的 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;

}










版权声明:本文首发在云栖社区,遵循云栖社区版权声明:本文内容由互联网用户自发贡献,版权归用户作者所有,云栖社区不为本文内容承担相关法律责任。云栖社区已升级为阿里云开发者社区。如果您发现本文中有涉嫌抄袭的内容,欢迎发送邮件至:developer2020@service.aliyun.com 进行举报,并提供相关证据,一经查实,阿里云开发者社区将协助删除涉嫌侵权内容。

分享:
云计算
使用钉钉扫一扫加入圈子
+ 订阅

时时分享云计算技术内容,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。

其他文章