美文网首页
method_exchange

method_exchange

作者: KevinMK | 来源:发表于2017-11-13 18:07 被阅读40次

背景

老是有莫名其妙的地方在修改我的逻辑,但是代码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

相关文章

  • method_exchange

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

  • swift runtime method_exchange方法

    由于项目都写完了需要适配ipad,网上搜索了一下,都有小问题 如果写在baseVC的present里面对基类代码侵...

网友评论

      本文标题:method_exchange

      本文链接:https://www.haomeiwen.com/subject/umazmxtx.html