wbcyclist/RuntimeInvoker

Invoke any method with name, lightweight NSInvocation wrapper

★ 0Forks 0GitHub ↗Compare

README

RuntimeInvoker

Language License

使用方法名称调用任何 Objective-C 对象的方法

目的

使用方法名称调用方法,可以用在一些奇技淫巧或是私有方法调试

用法

1. 编译运行程序

2. 在 ViewController.m 中找到用法

#import "ViewController.h"
#import "RuntimeInvoker.h"

@implementation ViewController

- (CGRect)aRect {
    return CGRectMake(0, 0, 100, 100);
}

+ (UIEdgeInsets)aInsets {
    return UIEdgeInsetsMake(0, 0, 100, 100);
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 公开方法
    CGRect rect = [[self invoke:@"aRect"] CGRectValue];
    NSLog(@"rect: %@", NSStringFromCGRect(rect));
    
    // 公开方法 & 参数传递
    [self.view invoke:@"setBackgroundColor:" arguments:@[ [UIColor whiteColor] ]];
    [self.view invoke:@"setAlpha:" arguments:@[ @(0.5) ]];
    [UIView animateWithDuration:3 animations:^{
        [self.view invoke:@"setAlpha:" arguments:@[ @(1.0) ]];
    }];
    
    // 私有方法
    int sizeClass = [[self invoke:@"_verticalSizeClass"] intValue];
    NSLog(@"sizeClass: %d", sizeClass);
    
    // 私有方法 & 参数传递
    [self invoke:@"_setShowingLinkPreview:" args:@(NO), nil];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self invoke:@"_setShowingLinkPreview:" args:@(YES), nil];
    });
    
    // 类方法
    UIEdgeInsets insets = [[self.class invoke:@"aInsets"] UIEdgeInsetsValue];
    NSLog(@"insets: %@", NSStringFromUIEdgeInsets(insets));
    
    // 类方法 & 参数传递
    UIColor *color = [UIColor invoke:@"colorWithRed:green:blue:alpha:"
                                args:@(0), @(0.5), @(1), nil];
    NSLog(@"color: %@", color);



    // 加密 [self printInfo]
    [self invoke:@"cHJpbnRJbmZv" selectorEncryptType:LANEncryptTypeBase64];
    
    // 加密 [ViewController printInfo]
    [@"ViewController" invokeClassMethod:@"cHJpbnRJbmZv" classEncryptType:LANEncryptTypeBase64 selectorEncryptType:LANEncryptTypeBase64];
}

-(void)printInfo {
    NSLog(@"this is -(void)printInfo selector");
}

+(void)printInfo {
    NSLog(@"this is +(void)printInfo selector");
}

@end

Contributors

cyanzhong0xlan

Issues