methodSwizzling的使用

来源:互联网 发布:java底层框架 编辑:程序博客网 时间:2024/06/06 20:50

之前公司的项目,产品经理要求,在项目中,集成页面统计,项目主要集成的友盟页面统计,至于友盟统计集成可参照友盟官网集成。
以下主要记录的是methodSwizzling在项目中的使用
Method swizzling 用于改变一个已经存在的selector的实现。这项技术使得在运行时通过改变 selector 在类的消息分发列表中的映射从而改变方法的实现成为可能。
例如:我们想要在一款 iOS app 中追踪每一个视图控制器被用户呈现了几次: 这可以通过在每个视图控制器的 viewDidAppear: 方法中添加追踪代码来实现,但这样会大量重复的样板代码。继承是另一种可行的方式,但是这要求所有被继承的视图控制器如 UIViewController, UITableViewController, UINavigationController 都在 viewDidAppear:实现追踪代码,这同样会造成很多重复代码。 如何让实现变得简化又不出现重复性代码,通过UIViewController的category分类使用methodSwizzling让这一想法得已实现,具体实现如下:

#import <objc/runtime.h>@implementation UIViewController (My)+ (void)load {    static dispatch_once_t onceToken;    dispatch_once(&onceToken, ^{        [UIViewController methodSwizzling:@selector(viewWillAppear:) swizzledSelector:@selector(cz_viewWillAppear:)];        [UIViewController methodSwizzling:@selector(viewWillDisappear:) swizzledSelector:@selector(cz_viewWillDisappear:)];        [UIViewController methodSwizzling:@selector(description) swizzledSelector:@selector(cz_description)];    });}//交换两个方法的实现。+ (void)methodSwizzling:(SEL)originalSelector swizzledSelector:(SEL)swizzledSelector {    Class class = [self class];    Method originalMethod = class_getInstanceMethod(class, originalSelector);    Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);    // When swizzling a class method, use the following:    // Class class = object_getClass((id)self);    // ...    // Method originalMethod = class_getClassMethod(class, originalSelector);    // Method swizzledMethod = class_getClassMethod(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);    }}#pragma mark - Swizzling Method- (void)cz_viewWillAppear:(BOOL)animated {    //先调用系统的实现。调用cz_viewWillAppear: 方法名,其实调用的是系统的实现。    [self cz_viewWillAppear:animated];    if (self.description.length) {        [MobClick beginLogPageView:self.description];        //        CZLog(@"viewWillAppear: %@", self);    }}- (void)cz_viewWillDisappear:(BOOL)animated {    //先调用系统的实现。调用cz_viewWillDisappear: 方法名,其实调用的是系统的实现。    [self cz_viewWillDisappear:animated];    if (self.description.length) {        [MobClick endLogPageView:self.description];        //        CZLog(@"viewWillDisappear: %@", self);    }}- (NSString *)cz_description {    //返回自定义导航栏的标题    if ([self.navigationBar isKindOfClass:[CZNavigationBar class]]) {        return self.navigationBar.title;    } else {        //导航控制器 self.navigationBar.title 会crash。        return @"";    }}

swizzling应该只在+load中完成, 在 Objective-C 的运行时中,+load 是在一个类被初始装载时调用。
swizzling 应该只在 dispatch_once 中完成,由于 swizzling 改变了全局的状态,所以我们需要确保每个预防措施在运行时都是可用的。原子操作就是这样一个用于确保代码只会被执行一次的预防措施,就算是在不同的线程中也能确保代码只执行一次。Grand Central Dispatch 的 dispatch_once 满足了所需要的需求,并且应该被当做使用 swizzling 的初始化单例方法的标准。

methodSwizzling中涉及到三个概念:
Selectors, Methods, & Implementations
理解 selector, method, implementation 这三个概念之间关系的最好方式是:在运行时,类(Class)维护了一个消息分发列表来解决消息的正确发送。每一个消息列表的入口是一个方法(Method),这个方法映射了一对键值对,其中键值是这个方法的名字 selector(SEL),值是指向这个方法实现的函数指针 implementation(IMP)。 Method swizzling 修改了类的消息分发列表使得已经存在的 selector 映射了另一个实现 implementation,同时重命名了原生方法的实现为一个新的 selector。

0 0
原创粉丝点击