Method Swizzling原理
在Objective-C中调用一个方法,其实是向一个对象发送消息,查找消息的唯一依据是selector的名字。利用Objective-C的动态特性,可以实现在运行时偷换selector对应的方法实现,达到给方法挂钩的目的。
每个类都有一个方法列表,存放着selector的名字和方法实现的映射关系。IMP有点类似函数指针,指向具体的Method实现。
我们可以利用 method_exchangeImplementations 来交换2个方法中的IMP,
我们可以利用 class_replaceMethod 来修改类,
我们可以利用 method_setImplementation 来直接设置某个方法的IMP,
……
归根结底,都是偷换了selector的IMP
实用教程
当你使用友盟的统计时,需要在每个VC的viewWillAppear和viewWillDisappear写固定的代码,虽然可以复制粘贴,但是还是很烦,还有就是在- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions这个方法里会写好多东西,会觉得很乱,所以接下来介绍一点小东西
-
1 新建一个UIViewController的类目
2 在.m中实现+load方法,这个方法会在初始化之前直接编译,然后在+load方法里使用runtime去交换2个方法中的IMP,当然那些注册激光推送的key,注册友盟的key,等一些代码,这样appDelegate里面的代码就会少很多,+load方法只会走一次,别的会按照生命周期来存在
+(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);
//更换方法的IMP
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:));
});
}
- 3 更换IMP的方法
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);
}
}
- 4 这样友盟的统计就没有必要每次都在VC里面写同样地代码
-(void)aop_viewWillAppear:(BOOL)animated {
[self aop_viewWillAppear:animated];
NSLog(@"黑魔法");
#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
}
- 5 也可以在viewDidLoad里面设置一些需要多次重复使用的代码
-(void)aop_viewDidLoad {
[self aop_viewDidLoad];
if ([self isKindOfClass:[UINavigationController class]]) {
///设置UINavigationController的一些属性,一个应用里面的UINavigationController基本是相同的
UINavigationController *nav = (UINavigationController *)self;
nav.navigationBar.translucent = NO;
nav.navigationBar.barTintColor = [UIColor redColor];
nav.navigationBar.tintColor = [UIColor whiteColor];
NSDictionary *titleAtt = @{NSForegroundColorAttributeName:[UIColor whiteColor]};
[[UINavigationBar appearance] setTitleTextAttributes:titleAtt];
[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60)forBarMetrics:UIBarMetricsDefault];
//修改系统返回按钮字体大小
NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:14],NSShadowAttributeName:[NSValue valueWithUIOffset:UIOffsetZero]};
[[UIBarButtonItem appearance] setTitleTextAttributes:attributes forState:UIControlStateNormal];
}
self.navigationController.interactivePopGestureRecognizer.delegate = (id<UIGestureRecognizerDelegate>)self;
}
参考代码: https://github.com/WWLJ/MethodSwizzling.git
参考文献: http://blog.csdn.net/yiyaaixuexi/article/details/9374411