基于Aspects的AOP埋点---使用plist文件加载内容

在这篇博客,看到了一个demo是基于Aspects的,再结合自己公司的埋点需求,决定用一个Plist文件解决慢点参数的传递。

原demo的传参是这样的:

NSDictionary *config = @{
        @"MainViewController": @{
              GLLoggingPageImpression: @"page imp - main page",
              GLLoggingTrackedEvents: @[
                      @{
                          GLLoggingEventName: @"button one clicked",
                          GLLoggingEventSelectorName: @"buttonOneClicked:",
                          GLLoggingEventHandlerBlock: ^(id aspectInfo) {
                              NSLog(@"button one clicked");
                          },
                        },
                      @{
                          GLLoggingEventName: @"button two clicked",
                          GLLoggingEventSelectorName: @"buttonTwoClicked:",
                          GLLoggingEventHandlerBlock: ^(id aspectInfo) {
                              NSLog(@"button two clicked");
                          },
                        },
                      ],
        },

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

照搬成plist:

 NSString *filePath = [[NSBundle mainBundle] pathForResource:@"plist" ofType:@"plist"];
 NSDictionary *config = [NSDictionary dictionaryWithContentsOfFile:filePath];

plist文件:

基于Aspects的AOP埋点---使用plist文件加载内容_第1张图片
image.png

发送参数到自己服务器

+ (void)setupWithConfiguration:(NSDictionary *)configs
{
    // Hook Page Impression
    [UIViewController aspect_hookSelector:@selector(viewDidAppear:)
                              withOptions:AspectPositionAfter
                               usingBlock:^(id aspectInfo) {
                                   dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                                       NSString *className = NSStringFromClass([[aspectInfo instance] class]);
                                       NSString *pageImp = configs[className][GLLoggingPageImpression];
                                       if (pageImp) {
                                           //将参数发到自己服务器
                                           NSLog(@"%@", pageImp);
                                       }
                                   });
                               } 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) {
                                    //将参数发到自己服务器
                                    NSLog(@"%@",event[GLLoggingEventHandlerBlock]);
                                    NSLog(@"%@",event[GLLoggingEventHandlerBlock2]);
//                                  dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//                                        block(aspectInfo);
//                                    });
                                } error:NULL];
                
            }
        }
    }
}

demo地址

你可能感兴趣的:(基于Aspects的AOP埋点---使用plist文件加载内容)