美文网首页
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

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