method_exchange

背景

老是有莫名其妙的地方在修改我的逻辑,但是代码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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,477评论 19 139
  • 转至元数据结尾创建: 董潇伟,最新修改于: 十二月 23, 2016 转至元数据起始第一章:isa和Class一....
    40c0490e5268阅读 5,842评论 0 9
  • 国家电网公司企业标准(Q/GDW)- 面向对象的用电信息数据交换协议 - 报批稿:20170802 前言: 排版 ...
    庭说阅读 13,871评论 6 13
  • 1,Search Bar 怎样去掉背景的颜色(storyboard里只能设置background颜色,可是发现cl...
    以德扶人阅读 7,541评论 2 50
  • 现在的家庭可能门槛并不那么明显了,在以前门槛还是比较明显的。但是不管明显还是不明显,门槛在风水中一直都是非常讲究的...
    我很撕心裂肺阅读 5,649评论 0 0