method_exchange

2017-11-13  本文已影响40人  KevinMK

背景

老是有莫名其妙的地方在修改我的逻辑,但是代码search找不出来,所以准备直接看看是哪儿调用的,因为是系统的方法,所以准备用method_exchange

需要替换的方法

[[UIApplication sharedApplication] setIdleTimerDisabled:NO];

首先交换方法

    Method orig_method = nil, alt_method = nil;
    
    SEL orig_sel = @selector(setIdleTimerDisabled:);
    SEL alt_sel = @selector(kk_setIdleTimerDisabled:);

    // First, look for the methods
    orig_method = class_getInstanceMethod([UIApplication class], orig_sel);
    alt_method = class_getInstanceMethod([UIApplication class], alt_sel);

    // If both are found, swizzle them
    if ((orig_method != nil) && (alt_method != nil)) {
        method_exchangeImplementations(orig_method, alt_method);
    }

实现一个分类

@interface UIApplication(KKUIDebug)

- (void)kk_setIdleTimerDisabled:(BOOL)idle;

@end

@implementation UIApplication (KKUIDebug)

- (void)kk_setIdleTimerDisabled:(BOOL)idle
{
    [self kk_setIdleTimerDisabled:idle];
}

@end
上一篇下一篇

猜你喜欢

热点阅读