"黑魔法"Method Swizzling和IMP指针

IMP指针

typedef void (*_VIMP)(id, SEL, ...);

@implementation UIViewController (category)

+ (void)load {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
//        获取原始方法
        Method viewWillAppear = class_getInstanceMethod(self, @selector(viewWillAppear:));
//        获取方法的实现
        _VIMP viewWillAppear_IMP = (_VIMP)method_getImplementation(viewWillAppear);
//        重新设置方法实现
        method_setImplementation(viewWillAppear, imp_implementationWithBlock(^(id target){
//            调用原有方法
        viewWillAppear_IMP(target,@selector(viewWillAppear:));
            [MobClick beginLogPageView:NSStringFromClass([target class])];
        }));
    //TODO...可以自己添加viewWillDisappear等等
    });
}

@end

“黑魔法”Method Swizzling

@implementation UIViewController (category)

+ (void)load {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        Class class = [self class];
        // When swizzling a class method, use the following:
        // Class class = object_getClass((id)self);
        swizzleMethod(class, @selector(viewDidLoad), @selector(aop_viewDidLoad));
        swizzleMethod(class, @selector(viewDidAppear:), @selector(aop_viewDidAppear:));
        swizzleMethod(class, @selector(viewWillAppear:), @selector(aop_viewWillAppear:));
        swizzleMethod(class, @selector(viewWillDisappear:), @selector(aop_viewWillDisappear:));
    });
}
void swizzleMethod(Class class, SEL originalSelector, SEL swizzledSelector)   {
    Method originalMethod = class_getInstanceMethod(class, originalSelector);
    Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
    BOOL didAddMethod =
    class_addMethod(class,
                    originalSelector,
                    method_getImplementation(swizzledMethod),
                    method_getTypeEncoding(swizzledMethod));
    if (didAddMethod) {
        class_replaceMethod(class,
                            swizzledSelector,
                            method_getImplementation(originalMethod),
                            method_getTypeEncoding(originalMethod));
    } else {
        method_exchangeImplementations(originalMethod, swizzledMethod);
    }
}

- (void)aop_viewDidAppear:(BOOL)animated {
    [self aop_viewDidAppear:animated];
}

-(void)aop_viewWillAppear:(BOOL)animated {
    [self aop_viewWillAppear:animated];
#ifndef DEBUG
    [MobClick beginLogPageView:NSStringFromClass([self class])];
#endif
}

-(void)aop_viewWillDisappear:(BOOL)animated {
    [self aop_viewWillDisappear:animated];
#ifndef DEBUG
    [MobClick endLogPageView:NSStringFromClass([self class])];
#endif
}

- (void)aop_viewDidLoad {
    [self aop_viewDidLoad];
}

@end

以上代码实现的功能是在每个界面出现的时候添加一个友盟页面停留时间统计功能

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值