在这篇博客,看到了一个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文件:
发送参数到自己服务器
+ (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地址