您的位置:首页 > 编程语言

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

2016-07-26 20:44 411 查看
// 运行时,这整的

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: