修改 hook 逻辑 review by daiqiang
Showing
1 changed file
with
24 additions
and
29 deletions
@@ -88,7 +88,7 @@ void yher_collectionViewDidSelectItemAtIndexPath(id self, SEL _cmd, id collectio | @@ -88,7 +88,7 @@ void yher_collectionViewDidSelectItemAtIndexPath(id self, SEL _cmd, id collectio | ||
88 | if (!view) { | 88 | if (!view) { |
89 | return; | 89 | return; |
90 | } | 90 | } |
91 | - | 91 | + |
92 | [YH_EventCollector trackAppClickWithView:self UICollectionView:collectionView didSelectItemAtIndexPath:indexPath]; | 92 | [YH_EventCollector trackAppClickWithView:self UICollectionView:collectionView didSelectItemAtIndexPath:indexPath]; |
93 | } @catch (NSException *exception) { | 93 | } @catch (NSException *exception) { |
94 | YHLog(@"%@ error: %@", self, exception); | 94 | YHLog(@"%@ error: %@", self, exception); |
@@ -100,38 +100,22 @@ void yher_collectionViewDidSelectItemAtIndexPath(id self, SEL _cmd, id collectio | @@ -100,38 +100,22 @@ void yher_collectionViewDidSelectItemAtIndexPath(id self, SEL _cmd, id collectio | ||
100 | [self yher_collectionViewSetDelegate:delegate]; | 100 | [self yher_collectionViewSetDelegate:delegate]; |
101 | 101 | ||
102 | @try { | 102 | @try { |
103 | - SEL swizzledMethodSEL = @selector(collectionView:didSelectItemAtIndexPath:); | ||
104 | - if (![delegate respondsToSelector:swizzledMethodSEL]) { | 103 | + SEL originMethodSEL = @selector(collectionView:didSelectItemAtIndexPath:); |
104 | + if (![delegate respondsToSelector:originMethodSEL]) { | ||
105 | return; | 105 | return; |
106 | } | 106 | } |
107 | 107 | ||
108 | - Class class = [delegate class]; | 108 | + Class delegateClass = [delegate class]; |
109 | 109 | ||
110 | - SEL originMethodSEL = NSSelectorFromString(@"yher_collectionViewDidSelectItemAtIndexPath"); | ||
111 | - if (class_addMethod(class, originMethodSEL, (IMP)yher_collectionViewDidSelectItemAtIndexPath, "v@:@@")) { | ||
112 | - | ||
113 | - Method dis_swizzledMethod = class_getInstanceMethod(class, swizzledMethodSEL); | ||
114 | - | ||
115 | - //若子类实现了swizzledMethodSEL,直接exchange; | ||
116 | - //若子类没有实现时,读取父类的方法实现,再添加到子类上, | ||
117 | - //如果添加成功,exchange实现,否则不交换 | ||
118 | - | ||
119 | - BOOL isLocal = [[self class] isLocallyDefinedMethod:dis_swizzledMethod onClass:class]; | ||
120 | - if (isLocal) { | ||
121 | - Method dis_originMethod = class_getInstanceMethod(class, originMethodSEL); | ||
122 | - method_exchangeImplementations(dis_originMethod, dis_swizzledMethod); | ||
123 | - }else{ | ||
124 | - if (class_addMethod(class, | ||
125 | - swizzledMethodSEL, | ||
126 | - class_getMethodImplementation(class, swizzledMethodSEL), | ||
127 | - method_getTypeEncoding(class_getInstanceMethod(class, swizzledMethodSEL)))) { | ||
128 | - | ||
129 | - dis_swizzledMethod = class_getInstanceMethod(class, swizzledMethodSEL); | ||
130 | - Method dis_originMethod = class_getInstanceMethod(class, originMethodSEL); | ||
131 | - method_exchangeImplementations(dis_originMethod, dis_swizzledMethod); | ||
132 | - } | ||
133 | - } | ||
134 | - | 110 | + SEL swizzleMethodSEL = NSSelectorFromString(@"yher_collectionViewDidSelectItemAtIndexPath"); |
111 | + | ||
112 | + Method originMethod = class_getInstanceMethod(delegateClass, originMethodSEL); | ||
113 | + | ||
114 | + Class class = GetSelectorInsClass(delegateClass,originMethodSEL,originMethod); | ||
115 | + | ||
116 | + if (class_addMethod(class, swizzleMethodSEL, (IMP)yher_collectionViewDidSelectItemAtIndexPath, "v@:@@")) { | ||
117 | + Method swizzleMethod = class_getInstanceMethod(class, swizzleMethodSEL); | ||
118 | + method_exchangeImplementations(originMethod, swizzleMethod); | ||
135 | } | 119 | } |
136 | 120 | ||
137 | } @catch (NSException *exception) { | 121 | } @catch (NSException *exception) { |
@@ -139,4 +123,15 @@ void yher_collectionViewDidSelectItemAtIndexPath(id self, SEL _cmd, id collectio | @@ -139,4 +123,15 @@ void yher_collectionViewDidSelectItemAtIndexPath(id self, SEL _cmd, id collectio | ||
139 | } | 123 | } |
140 | } | 124 | } |
141 | 125 | ||
126 | +Class GetSelectorInsClass(Class hclass, SEL sel, Method sel_method) { | ||
127 | + Class super_class = [hclass superclass]; | ||
128 | + if (!super_class) { | ||
129 | + return hclass; | ||
130 | + } | ||
131 | + Method method = class_getInstanceMethod(super_class, sel); | ||
132 | + if (method != sel_method) { | ||
133 | + return hclass; | ||
134 | + } | ||
135 | + return GetSelectorInsClass(super_class, sel, sel_method); | ||
136 | +} | ||
142 | @end | 137 | @end |
-
Please register or login to post a comment