美文网首页
Aspects 源码分析

Aspects 源码分析

作者: FlyTheKite | 来源:发表于2017-07-17 22:16 被阅读6次

Method Swizzling 和 AOP 实践

// the method might not exist in the class, but in its superclass
    Method originalMethod = class_getInstanceMethod(class, originalSelector);
    Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);

    // class_addMethod will fail if original method already exists
    BOOL didAddMethod = class_addMethod(class, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod));

    // the method doesn’t exist and we just added one
    if (didAddMethod) {
        class_replaceMethod(class, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
    } 
    else {
        method_exchangeImplementations(originalMethod, swizzledMethod);
    }


这里唯一可能需要解释的是 class_addMethod 。要先尝试添加原 selector 是为了做一层保护,因为如果这个类没有实现 originalSelector ,但其父类实现了,那 class_getInstanceMethod 会返回父类的方法。这样 method_exchangeImplementations 替换的是父类的那个方法,这当然不是你想要的。所以我们先尝试添加 orginalSelector ,如果已经存在,再用 method_exchangeImplementations 把原方法的实现跟新的方法实现给交换掉。

你可以用同样的方式在任何你感兴趣的方法里添加自定义代码,比如 IBAction 的方法里。更好的方式,你提供一个 Logging 的配置文件作为唯一处理事件记录的地方:

@implementation AppDelegate (Logging)

+ (void)setupLogging
{
    NSDictionary *config = @{
        @"MainViewController": @{
            GLLoggingPageImpression: @"page imp - main page",
            GLLoggingTrackedEvents: @[
                @{
                    GLLoggingEventName: @"button one clicked",
                    GLLoggingEventSelectorName: @"buttonOneClicked:",
                    GLLoggingEventHandlerBlock: ^(id<AspectInfo> aspectInfo) {
                        [Logging logWithEventName:@"button one clicked"];
                    },
                },
                @{
                    GLLoggingEventName: @"button two clicked",
                    GLLoggingEventSelectorName: @"buttonTwoClicked:",
                    GLLoggingEventHandlerBlock: ^(id<AspectInfo> aspectInfo) {
                        [Logging logWithEventName:@"button two clicked"];
                    },
                },
           ],
        },

        @"DetailViewController": @{
            GLLoggingPageImpression: @"page imp - detail page",
        }
    };

    [AppDelegate setupWithConfiguration:config];
}

+ (void)setupWithConfiguration:(NSDictionary *)configs
{
    // Hook Page Impression
    [UIViewController aspect_hookSelector:@selector(viewDidAppear:)
                              withOptions:AspectPositionAfter
                               usingBlock:^(id<AspectInfo> aspectInfo) {
                                       NSString *className = NSStringFromClass([[aspectInfo instance] class]);
                                    [Logging logWithEventName:className];
                               } error:NULL];

    // Hook Events
    for (NSString *className in configs) {
        Class clazz = NSClassFromString(className);
        NSDictionary *config = configs[className];

        if (config[GLLoggingTrackedEvents]) {
            for (NSDictionary *event in config[GLLoggingTrackedEvents]) {
                SEL selekor = NSSelectorFromString(event[GLLoggingEventSelectorName]);
                AspectHandlerBlock block = event[GLLoggingEventHandlerBlock];

                [clazz aspect_hookSelector:selekor
                               withOptions:AspectPositionAfter
                                usingBlock:^(id<AspectInfo> aspectInfo) {
                                    block(aspectInfo);
                                } error:NULL];

            }
        }
    }
}
然后在 -application:didFinishLaunchingWithOptions: 里调用 setupLogging:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.

    [self setupLogging];
    return YES;
}

相关文章

  • iOS 引入 Aspects 吧,迟早会用到!

    引入 Aspects 吧,迟早会用到! 关于 AOP 在 iOS 中的应用,以及 Aspects 的源码分析文章...

  • Aspects 源码分析

    Method Swizzling 和 AOP 实践 这里唯一可能需要解释的是 class_addMethod 。要...

  • Aspects源码分析

    Aspects一直在项目中使用,主要使用AOP方式进行tracking,但还没好好看一看,最近研究了一下源码,十分...

  • Aspects 源码分析

    Aspects 分析- (id)aspect_hookSelector:(SEL)sel...

  • Aspects 源码分析

    需求 hook 实例对象的方法(仅该对象的方法被hook) hook 类对象的方法(该类所有对象调用该方法的时候,...

  • IOS中AOP框架Aspects源码分析

    IOS中AOP框架Aspects源码分析 AOP是Aspect Oriented Programming的缩写,意...

  • Aspects源码浅析

    Aspects源码浅析 同步发布到博客地址Aspects源码浅析 Aspects 可以很方便的让我们 hook 要...

  • Aspects源码解析

    Aspects 源码:https://github.com/steipete/Aspects 基本概述,此框架提供...

  • Aspects 源码解读

    Aspects 源码解读 1.Aspects简介 Aspects是一种面向切面编程,相对于继承而已,无需改动目标源...

  • Aspects改进尝试

    背景 一个库:Aspects两篇文章:面向切面编程之 Aspects 源码解析及应用消息转发机制与Aspects源...

网友评论

      本文标题:Aspects 源码分析

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