在Mixpanel中事件绑定触发是由web端可视化埋点发送bindingRequest类型请求数据,移动端接受后尝试绑定,如果绑定顺利,反馈给web端,web端把绑定数据上传到服务器中。
当我们打开应用APP,初始化时候会请求服务端接口,服务端下发数据到每台APP中,这样每台手机的APP都能实现相同埋点功能,这就是可视化埋点的魅力了。
那么,数据传输到客户端后,是通过怎样的方式找到控件呢?找到控件后是如何给控件增加点击事件呢?
通过Path寻找绑定控件的过程
和序列化有所不同的是,事件绑定是从根控制器开始:[[Mixpanel sharedUIApplication] keyWindow].rootViewController。可能是因为序列化是从当前屏幕下方开始处理,而事件绑定可能会有多个界面,绑定多个界面的控件,当前屏幕满足不了我们需求。mixpanel很好的处理了这个问题,只是目前不太清楚web端是如何来反序列化的到路径问题的,这个还有待研究。
执行绑定
MPEventBinding类中提供了execute与stop两个子类需要覆写的接口。execute的主要作用是在筛选出的控件上绑定后续要发的事件。而stop的作用正好相反,是从已经绑定了事件的控件上删除绑定事件。不同的控件有着不同的触发方式,这里抽象成了MPUIControlBinding与MPUITableViewBinding。对于UIControl对象,使用了addTarget:action:forControlEvents:将binding对象及行为与控件绑定在一块。在用户点击按键时,自然触发绑定事件。同时需要覆写UIView的didMoveToWindow和didMoveToSuperview方法,用于在新的UIControl对象创建时判断时否需要绑定现有的binding对象。
- (void)execute
{
if (!self.appliedTo) {
[self resetAppliedTo];
}
if (!self.running) {
void (^executeBlock)(id, SEL) = ^(id view, SEL command) {
[NSThread mp_safelyRunOnMainThreadSync:^{
NSArray *objects;
NSObject *root = [[Mixpanel sharedUIApplication] keyWindow].rootViewController;
if (view && [self.appliedTo containsObject:view]) {
if (![self.path fuzzyIsLeafSelected:view fromRoot:root]) {
[self stopOnView:view];
[self.appliedTo removeObject:view];
}
} else {
// select targets based off path
if (view) {
if ([self.path fuzzyIsLeafSelected:view fromRoot:root]) {
objects = @[view];
} else {
objects = @[];
}
} else {
objects = [self.path fuzzySelectFromRoot:root];
}
for (UIControl *control in objects) {
if ([control isKindOfClass:[UIControl class]]) {
if (self.verifyEvent != 0 && self.verifyEvent != self.controlEvent) {
[control addTarget:self
action:@selector(preVerify:forEvent:)
forControlEvents:self.verifyEvent];
}
[control addTarget:self
action:@selector(execute:forEvent:)
forControlEvents:self.controlEvent];
[self.appliedTo addObject:control];
}
}
}
}];
};
executeBlock(nil, _cmd);
[MPSwizzler swizzleSelector:NSSelectorFromString(@"didMoveToWindow")
onClass:self.swizzleClass
withBlock:executeBlock
named:self.name];
[MPSwizzler swizzleSelector:NSSelectorFromString(@"didMoveToSuperview")
onClass:self.swizzleClass
withBlock:executeBlock
named:self.name];
self.running = true;
}
}
模糊查询:
在MPObjectSelector类中的nextFilter方法中,该字符串会根据‘/’被解析成若干个MPObectFilter对象保存在filters数组中。遍历_filters的到每一个filter(枝干),我们查询的过程是将对象的类和枝干name进行匹配,匹配方向有自上而下、自下及上两种方式,
- (NSArray *)selectFromRoot:(id)root evaluatingFinalPredicate:(BOOL)finalPredicate
{
NSArray *views = @[];
if (root) {
views = @[root];
NSUInteger i = 0, n = _filters.count;
for (MPObjectFilter *filter in _filters) {
filter.nameOnly = (i == n-1 && !finalPredicate);
views = [filter apply:views];
if (views.count == 0) {
break;
}
i++;
}
}
return views;
}
筛选
通过我们模糊查询找到了符合条件的对象,模糊查询要求类别一致即可。接下来我们做特征值的筛选功能:控件特征值通过predicate进行对比找到我们要绑定的控件。该特征值正是我们获取到的控件关键信息。
- (NSArray *)apply:(NSArray *)views
{
NSMutableArray *result = [NSMutableArray array];
Class class = NSClassFromString(_name);
if (class || [_name isEqualToString:@"*"]) {
// Select all children
for (NSObject *view in views) {
NSArray *children = [self getChildrenOfObject:view ofType:class];
if (_index && _index.unsignedIntegerValue < children.count) {
// Indexing can only be used for subviews of UIView
if ([view isKindOfClass:[UIView class]]) {
children = @[children[_index.unsignedIntegerValue]];
} else {
children = @[];
}
}
[result addObjectsFromArray:children];
}
}
if (!self.nameOnly) {
// If unique is set and there are more than one, return nothing
if (self.unique && result.count != 1) {
return @[];
}
// Filter any resulting views by predicate
if (self.predicate) {
return [result filteredArrayUsingPredicate:self.predicate];
}
}
return [result copy];
}
- (NSArray *)getChildrenOfObject:(NSObject *)obj ofType:(Class)class
{
NSMutableArray *children = [NSMutableArray array];
// A UIWindow is also a UIView, so we could in theory follow the subviews chain from UIWindow, but
// for now we only follow rootViewController from UIView.
if ([obj isKindOfClass:[UIWindow class]]) {
UIViewController *rootViewController = ((UIWindow *)obj).rootViewController;
if ([rootViewController isKindOfClass:class]) {
[children addObject:rootViewController];
}
} else if ([obj isKindOfClass:[UIView class]]) {
// NB. For UIViews, only add subviews, nothing else.
// The ordering of this result is critical to being able to
// apply the index filter.
NSArray *subviews = [[(UIView *)obj subviews] copy];
for (NSObject *child in subviews) {
if (!class || [child isKindOfClass:class]) {
[children addObject:child];
}
}
} else if ([obj isKindOfClass:[UIViewController class]]) {
UIViewController *viewController = (UIViewController *)obj;
for (NSObject *child in [viewController childViewControllers]) {
if (!class || [child isKindOfClass:class]) {
[children addObject:child];
}
}
UIViewController *presentedViewController = viewController.presentedViewController;
if (presentedViewController && (!class || [presentedViewController isKindOfClass:class])) {
[children addObject:presentedViewController];
}
if (!class || (viewController.isViewLoaded && [viewController.view isKindOfClass:class])) {
[children addObject:viewController.view];
}
}
NSArray *result;
// Reorder the cells in a table view so that they are arranged by y position
if ([class isSubclassOfClass:[UITableViewCell class]]) {
result = [children sortedArrayUsingComparator:^NSComparisonResult(UIView *obj1, UIView *obj2) {
if (obj2.frame.origin.y > obj1.frame.origin.y) {
return NSOrderedAscending;
} else if (obj2.frame.origin.y < obj1.frame.origin.y) {
return NSOrderedDescending;
}
return NSOrderedSame;
}];
} else {
result = [children copy];
}
return result;
}
网友评论