无埋点核心技术:iOS Hook在字节的实践经验

{"type":"doc","content":[{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"前言"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"众所周知,精确的推荐离不开大量埋点,常见的埋点采集方案是在响应用户行为操作的路径上进行埋点。但是由于 App 通常会有比较多界面和操作路径,主动埋点的维护成本就会非常大。所以行业的做法是无埋点,而无埋点实现需要 AOP 编程。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"一个常见的场景,比如想在"},{"type":"codeinline","content":[{"type":"text","text":"UIViewController"}]},{"type":"text","text":"出现和消失的时刻分别记录时间戳用于统计页面展现的时长。要达到这个目标有很多种方法,但是 AOP 无疑是最简单有效的方法。Objective-C 的 Hook 其实也有很多种方式,这里以 Method Swizzle 给个示例。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"@interface UIViewController (MyHook)\n\n@end\n\n@implementation UIViewController (MyHook)\n\n+ (void)load {\n static dispatch_once_t onceToken;\n dispatch_once(&onceToken, ^{\n \/\/\/ 常规的 Method Swizzle封装\n swizzleMethods(self, @selector(viewDidAppear:), @selector(my_viewDidAppear:));\n \/\/\/ 更多Hook\n });\n}\n\n- (void)my_viewDidAppear:(BOOL)animated {\n \/\/\/ 一些Hook需要的逻辑\n\n \/\/\/ 这里调用Hook后的方法,其实现其实已经是原方法了。\n [self my_viewDidAppear: animated];\n}\n\n@end"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"接下来我们探讨一个具体场景:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"UICollectionView"}]},{"type":"text","text":"或者"},{"type":"codeinline","content":[{"type":"text","text":"UITableView"}]},{"type":"text","text":"是 iOS 中非常常用的列表 UI 组件,其中列表元素的点击事件回调是通过"},{"type":"codeinline","content":[{"type":"text","text":"delegate"}]},{"type":"text","text":"完成的。这里以"},{"type":"codeinline","content":[{"type":"text","text":"UICollectionView"}]},{"type":"text","text":"为例,"},{"type":"codeinline","content":[{"type":"text","text":"UICollectionView"}]},{"type":"text","text":"的"},{"type":"codeinline","content":[{"type":"text","text":"delegate"}]},{"type":"text","text":",有个方法声明,"},{"type":"codeinline","content":[{"type":"text","text":"collectionView:didSelectItemAtIndexPath:"}]},{"type":"text","text":",实现这个方法我们就可以给列表元素添加点击事件。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"我们的目标是 Hook 这个 delegate 的方法,在点击回调的时候进行额外的埋点操作。"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"方案迭代"}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"方案 1 Method Swizzle"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"通常情况下,Method Swizzle 可以满足绝大部分的 AOP 编程需求。因此首次迭代,我们直接使用 Method Swizzle 来进行 Hook。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"@interface UICollectionView (MyHook)\n\n@end\n\n@implementation UICollectionView (MyHook)\n\n\/\/ Hook, setMyDelegate:和setDelegate:交换过\n- (void)setMyDelegate:(id)delegate {\n if (delegate != nil) {\n \/\/\/ 常规Method Swizzle\n swizzleMethodsXXX(delegate, @selector(collectionView:didSelectItemAtIndexPath:), self, @selector(my_collectionView:didSelectItemAtIndexPath:));\n\n }\n\n [self setMyDelegate:nil];\n}\n\n- (void)my_collectionView:(UICollectionView *)ccollectionView didSelectItemAtIndexPath:(NSIndexPath *)index {\n \/\/\/ 一些Hook需要的逻辑\n\n \/\/\/ 这里调用Hook后的方法,其实现其实已经是原方法了。\n [self my_collectionView:ccollectionView didSelectItemAtIndexPath:index];\n}\n\n@end"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我们把这个方案集成到今日头条 App 里面进行测试验证,发现没法办法验证通过。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"主要原因今日头条 App 是一个庞大的项目,其中引入了非常多的三方库,比如 IGListKit 等,这些三方库通常对"},{"type":"codeinline","content":[{"type":"text","text":"UICollectionView"}]},{"type":"text","text":"的使用都进行了封装,而这些封装,恰恰导致我们不能使用常规的 Method Swizzle 来 Hook 这个 delegate。直接的原因总结有以下两点:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"numberedlist","attrs":{"start":null,"normalizeStart":1},"content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"setDelegate"}]},{"type":"text","text":"传入的对象不是实现"},{"type":"codeinline","content":[{"type":"text","text":"UICollectionViewDelegate"}]},{"type":"text","text":"协议的那个对象"}]}]}]},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/infoq\/64\/6460545a1108207a658db20fe88dccff.png","alt":"图片","title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如图示,"},{"type":"codeinline","content":[{"type":"text","text":"setDelegate"}]},{"type":"text","text":"传入的是一个代理对象 proxy,proxy 引用了实际的实现"},{"type":"codeinline","content":[{"type":"text","text":"UICollectionViewDelegate"}]},{"type":"text","text":"协议的"},{"type":"codeinline","content":[{"type":"text","text":"delegate"}]},{"type":"text","text":",proxy 实际上并没有实现"},{"type":"codeinline","content":[{"type":"text","text":"UICollectionViewDelegate"}]},{"type":"text","text":"的任何一个方法,它把所有方法都转发给实际的"},{"type":"codeinline","content":[{"type":"text","text":"delegate"}]},{"type":"text","text":"。这种情况下,我们不能直接对 proxy 进行 Method Swizzle"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"numberedlist","attrs":{"start":null,"normalizeStart":1},"content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"text","text":"多次"},{"type":"codeinline","content":[{"type":"text","text":"setDelegate"}]}]}]}]},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/infoq\/64\/6460545a1108207a658db20fe88dccff.png","alt":"图片","title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在上述图例中,使用方存在连续调用两次"},{"type":"codeinline","content":[{"type":"text","text":"setDelegate"}]},{"type":"text","text":"的情况,第一次是真实"},{"type":"codeinline","content":[{"type":"text","text":"delegate"}]},{"type":"text","text":",第二次是"},{"type":"codeinline","content":[{"type":"text","text":"proxy"}]},{"type":"text","text":",我们需要区别对待。"}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"代理模式和 NSProxy 介绍"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"使用 proxy 对原对象进行代理,在处理完额外操作之后再调用原对象,这种模式称为代理模式。而 Objective-C 中要实现代理模式,使用 NSProxy 会比较高效。详细内容参考下列文章。"}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"代理模式"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"NSProxy 使用"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"这里面"},{"type":"codeinline","content":[{"type":"text","text":"UICollectionView"}]},{"type":"text","text":"的"},{"type":"codeinline","content":[{"type":"text","text":"setDelegate"}]},{"type":"text","text":"传入的是一个"},{"type":"codeinline","content":[{"type":"text","text":"proxy"}]},{"type":"text","text":"是非常常见的操作,比如 IGListKit,同时 App 基于自身需求,也有可能会做这一层封装。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在"},{"type":"codeinline","content":[{"type":"text","text":"UICollectionView"}]},{"type":"text","text":"的"},{"type":"codeinline","content":[{"type":"text","text":"setDelegate"}]},{"type":"text","text":"的时候,把"},{"type":"codeinline","content":[{"type":"text","text":"delegate"}]},{"type":"text","text":"包裹在"},{"type":"codeinline","content":[{"type":"text","text":"proxy"}]},{"type":"text","text":"中,然后把 proxy 设置给"},{"type":"codeinline","content":[{"type":"text","text":"UICollectionView"}]},{"type":"text","text":",使用"},{"type":"codeinline","content":[{"type":"text","text":"proxy"}]},{"type":"text","text":"对"},{"type":"codeinline","content":[{"type":"text","text":"delegate"}]},{"type":"text","text":"进行消息转发。"}]},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/infoq\/64\/6460545a1108207a658db20fe88dccff.png","alt":"图片","title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"方案 2 使用代理模式"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"方案 1 已经无法满足我们的需求了,我们考虑到既然对"},{"type":"codeinline","content":[{"type":"text","text":"delegate"}]},{"type":"text","text":"进行代理是一种常规操作,我们何不也使用代理模式,对"},{"type":"codeinline","content":[{"type":"text","text":"proxy"}]},{"type":"text","text":"再次代理。"}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"代码实现"}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"先 Hook "},{"type":"codeinline","content":[{"type":"text","text":"UICollectionView"}]},{"type":"text","text":"的"},{"type":"codeinline","content":[{"type":"text","text":"setDelegate"}]},{"type":"text","text":"方法"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"代理"},{"type":"codeinline","content":[{"type":"text","text":"delegate"}]}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"简单的代码示意如下"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"\/\/\/ 完整封装了一些常规的消息转发方法\n@interface DelegateProxy : NSProxy\n\n@property (nonatomic, weak, readonly) id target;\n\n@end\n\n\/\/\/ 为 CollectionView delegate转发消息的proxy\n@interface BDCollectionViewDelegateProxy : DelegateProxy\n\n@end\n\n@implementation BDCollectionViewDelegateProxy \n\n- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {\n \/\/track event here\n if ([self.target respondsToSelector:@selector(collectionView:didSelectItemAtIndexPath:)]) {\n [self.target collectionView:collectionView didSelectItemAtIndexPath:indexPath];\n\n }\n}\n\n- (BOOL)bd_isCollectionViewTrackerDecorator {\n return YES;\n}\n\n\/\/ 还有其他的消息转发的代码 先忽略\n- (BOOL)respondsToSelector:(SEL)aSelector {\n if (aSelector == @selector(bd_isCollectionViewTrackerDecorator)) {\n return YES;\n }\n\n return [self.target respondsToSelector:aSelector];\n}\n\n\n@end\n\n@interface UICollectionView (MyHook)\n\n@end\n\n@implementation UICollectionView (MyHook)\n\n- (void) setDd_TrackerProxy:(BDCollectionViewDelegateProxy *)object {\n objc_setAssociatedObject(self, @selector(bd_TrackerProxy), object, OBJC_ASSOCIATION_RETAIN_NONATOMIC);\n}\n\n- (BDCollectionViewDelegateProxy *) bd_TrackerProxy {\n BDCollectionViewDelegateProxy *bridge = objc_getAssociatedObject(self, @selector(bd_TrackerProxy));\n\n return bridge;\n}\n\n\/\/ Hook, setMyDelegate:和setDelegate:交换过了\n- (void)setMyDelegate:(id)delegate {\n if (delegate == nil) {\n [self setMyDelegate:delegate];\n return\n }\n\n \/\/ 不会释放,不重复设置\n if ([delegate respondsToSelector:@selector(bd_isCollectionViewTrackerDecorator)]) {\n [self setMyDelegate:delegate];\n return;\n }\n\n BDCollectionViewDelegateProxy *proxy = [[BDCollectionViewDelegateProxy alloc] initWithTarget:delegate];\n [self setMyDelegate:proxy];\n self.bd_TrackerProxy = proxy;\n\n}\n\n@end"}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"模型"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"下图实线表示强引用,虚线表示弱引用。"}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"情况一"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果使用方没有对"},{"type":"codeinline","content":[{"type":"text","text":"delegate"}]},{"type":"text","text":"进行代理,而我们使用代理模式"}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"UICollectionView"}]},{"type":"text","text":",其"},{"type":"codeinline","content":[{"type":"text","text":"delegate"}]},{"type":"text","text":"指针指向 DelegateProxy"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"DelegateProxy,被 UICollectionView 用 runtime 的方式强引用,其 target 弱引用真实 Delegate"}]}]}]},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/infoq\/64\/6460545a1108207a658db20fe88dccff.png","alt":"图片","title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"情况二"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果使用方也对"},{"type":"codeinline","content":[{"type":"text","text":"delegate"}]},{"type":"text","text":"进行代理,我们使用代理模式"}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我们只需要保证我们的 DelegateProxy 处于代理链中的一环即可"}]}]}]},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/infoq\/57\/57a55e92caf5c31ac6b7f5646370d8f5.png","alt":"图片","title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"从这里我们可以看出,代理模式有很好的扩展性,它允许代理链不断嵌套,只要我们都遵循代理模式的原则即可。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"到这里,我们的方案已经在今日头条 App 上测试通过了。但是事情远还没有结束。"}]},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"踩坑之旅"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"目前的还算比较可以,但是也不能完全避免问题。这里其实不仅仅是"},{"type":"text","marks":[{"type":"strong"}],"text":"UICollectionView 的 delegate,包括:"}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"UIWebView"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"WKWebView"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"UITableView"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"UICollectionView"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"UIScrollView"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"UIActionSheet"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"UIAlertView"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我们都采用相同的方法来进行 Hook。"},{"type":"text","marks":[{"type":"strong"}],"text":"同时我们将方案封装一个 SDK 对外提供,以下统称为 MySDK。"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"第一次踩坑"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"某客户接入我们的方案之后,在集成过程中反馈有必现 Crash,下面详细介绍一下这一次踩坑的经历。"}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"堆栈信息"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"重点信息是"},{"type":"codeinline","content":[{"type":"text","text":"[UIWebView webView:decidePolicyForNavigationAction:request:frame:decisionListener:]"}]},{"type":"text","text":"。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"Thread 0 Crashed:\n\n0 libobjc.A.dylib 0x000000018198443c objc_msgSend + 28\n\n1 UIKit 0x000000018be05b4c -[UIWebView webView:decidePolicyForNavigationAction:request:frame:decisionListener:] + 200\n\n2 CoreFoundation 0x0000000182731cd0 __invoking___ + 144\n\n3 CoreFoundation 0x000000018261056c -[NSInvocation invoke] + 292\n\n4 CoreFoundation 0x000000018261501c -[NSInvocation invokeWithTarget:] + 60\n\n5 WebKitLegacy 0x000000018b86d654 -[_WebSafeForwarder forwardInvocation:] + 156"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"从堆栈信息不难判断出 crash 原因是 UIWebView 的 delegate 野指针,那为啥出现野指针呢?"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"这里先说明一下 crash 的直接原因,然后再来具体分析为什么就出现了问题。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"numberedlist","attrs":{"start":null,"normalizeStart":1},"content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"text","text":"MySDK 对 setDelegate 进行了 Hook"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"text","text":"客户也对 setDelegate 进行了 Hook"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":3,"align":null,"origin":null},"content":[{"type":"text","text":"先执行 MySDK 的 Hook 逻辑调用,然后执行客户的 Hook 逻辑调用"}]}]}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"客户 Hook 的代码"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"@interface UIWebView (JSBridge)\n\n@end\n\n@implementation UIWebView (JSBridge)\n\n- (void)setJsBridge:(id)object {\n objc_setAssociatedObject(self, @selector(jsBridge), object, OBJC_ASSOCIATION_RETAIN_NONATOMIC);\n}\n\n- (WebViewJavascriptBridge *)jsBridge {\n WebViewJavascriptBridge *bridge = objc_getAssociatedObject(self, @selector(jsBridge));\n return bridge;\n}\n\n+ (void)load {\n static dispatch_once_t onceToken;\n dispatch_once(&onceToken, ^{\n swizzleMethods(self, @selector(setDelegate:), @selector(setJSBridgeDelegate:));\n swizzleMethods(self, @selector(initWithFrame:), @selector(initJSWithFrame:));\n });\n\n}\n\n- (instancetype)initJSWithFrame:(CGRect)frame {\n self = [self initJSWithFrame:frame];\n if (self) {\n WebViewJavascriptBridge *bridge = [WebViewJavascriptBridge bridgeForWebView:self];\n [self setJsBridge:bridge];\n }\n return self;\n}\n\n\/\/\/ webview.delegate = xxx 会被调用多次且传入的对象不一样\n- (void)setJSBridgeDelegate:(id)delegate {\n WebViewJavascriptBridge *bridge = self.jsBridge;\n if (delegate == nil || bridge == nil) {\n [self setJSBridgeDelegate:delegate];\n } else if (bridge == delegate) {\n [self setJSBridgeDelegate:delegate];\n } else {\n \/\/\/ 第一次进入这里传入 bridge\n \/\/\/ 第二次进入这里传入一个delegate\n if (![delegate isKindOfClass:[WebViewJavascriptBridge class]]) {\n [bridge setWebViewDelegate:delegate];\n \/\/\/ 下面这一行代码是客户缺少的\n \/\/\/ fix with this\n [self setJSBridgeDelegate:bridge];\n } else {\n [self setJSBridgeDelegate:delegate];\n }\n }\n}\n\n@end"}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"MySDK Hook 代码"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"@interface UIWebView (MyHook)\n\n@end\n\n@implementation UIWebView (MyHook)\n\n\/\/ Hook, setWebViewDelegate:和setDelegate:交换过\n- (void)setWebViewDelegate:(id)delegate {\n if (delegate == nil) {\n [self setWebViewDelegate:delegate];\n }\n BDWebViewDelegateProxy *proxy = [[BDWebViewDelegateProxy alloc] initWithTarget:delegate];\n self.bd_TrackerDecorator = proxy;\n [self setWebViewDelegate:proxy];\n}\n\n@end"}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"野指针原因"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"UIWebView 有两次调用 setDelegate 方法,第一次是传的 WebViewJavascriptBridge,第二次传的另一个实际的 WebViewDelegate。暂且称第一次传了 bridge 第二次传了实际上的 delegate。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"numberedlist","attrs":{"start":null,"normalizeStart":1},"content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"text","text":"第一次调用,MySDK Hook 的时候会用 DelegateProxy 包装住 bridge,所有方法通过 DelegateProxy 转发到 bridge,这里传给 "},{"type":"codeinline","content":[{"type":"text","text":"setJSBridgeDelegate:(id)delegate"}]},{"type":"text","text":"的 delegate 实际上是 DelegateProxy"},{"type":"text","marks":[{"type":"strong"}],"text":"而非 bridge"},{"type":"text","text":"。"}]}]}]},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/infoq\/07\/076c25fe31e1c0065b11d31a358ad6ff.png","alt":"图片","title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"这里需要注意,UIWebView 的 delegate 指向 DelegateProxy 是客户给设置上的,且这个属性"},{"type":"text","marks":[{"type":"strong"}],"text":"assign 而非 weak,这个 assign 很关键,assigin 在对象释放之后不会自动变为 nil。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"numberedlist","attrs":{"start":null,"normalizeStart":1},"content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"text","text":"第二次调用,MySDK Hook 的时候会用新的 DelegateProxy 包装住 delegate 也就是 WebViewDelegate,这个时候 MySDK 的逻辑是把新的 DelegateProxy 给强引用中,老的 DelegateProxy 就失去了强引用因此释放了。"}]}]}]},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/infoq\/27\/271914de5fb6ed8e7228ccfafa7ceab6.png","alt":"图片","title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"此时的状态如果不做任何处理,当前状态就如图示:"}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"delegate 指向已经释放的 DelegateProxy,野指针"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"UIWebview 触发回调就导致 crash"}]}]}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"修复方法"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果补上那一句,"},{"type":"codeinline","content":[{"type":"text","text":"setJSBridgeDelegate:(id)delegate"}]},{"type":"text","text":"在判断了 delegate 不是 bridge 之后,把 UIWebView 的 delegate 设置为 bridge 就可以完成了。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"注释中 fix with this 下一行代码"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"修复后模型如下图"}]},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/infoq\/46\/46aca0f5cf89eb7abc17b2c2819b8de2.png","alt":"图片","title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"总结"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"使用 Proxy 的方式虽然也可以解决一定的问题,但是也需要使用方遵循一定的规范,要意识到第三方 SDK 也可能"},{"type":"codeinline","content":[{"type":"text","text":"setDelegate"}]},{"type":"text","text":"进行 Hook,也可能使用 Proxy"}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"第二次踩坑"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"先补充一些参考资料"}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"RxCocoa 源码参考 https:\/\/github.com\/ReactiveX\/RxSwift"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"rxcocoa 学习-DelegateProxy"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"RxCocoa 也使用了代理模式,对 delegate 进行了代理,按道理应该没有问题。但是 RxCocoa 的实现有点出入。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":"br"}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/infoq\/6a\/6adecde829e551ce8643bdac7bf5ef6a.png","alt":"图片","title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果单独只使用了"},{"type":"text","marks":[{"type":"strong"}],"text":"RxCocoa"},{"type":"text","text":"的方案,和方案是一致,也就不会有任何问题。"}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"RxCocoa+MySDK"}]},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/infoq\/5e\/5ebd347129f97b743e4f15f709fe19b6.png","alt":"图片","title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"RxCocoa+MySDK 之后,变成这样子。UICollectionView 的 delegate 直接指向谁在于谁调用的"},{"type":"codeinline","content":[{"type":"text","text":"setDelegate"}]},{"type":"text","text":"方法后调。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"理论也应该没有问题,就是引用链多一个 poxy 包装而已。但是实际上有两个问题。"}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"问题 1"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"RxCocoa 的 delegate 的 get 方法命中 assert"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"\/\/ UIScrollView+Rx.swift\nextension Reactive where Base: UIScrollView {\n public var delegate: DelegateProxy {\n return RxScrollViewDelegateProxy.proxy(for: base)\n \/\/ base可以理解为一个UIScrollView 实例\n }\n}\n\nopen class RxScrollViewDelegateProxy {\n public static func proxy(for object: ParentObject) -> Self {\n let maybeProxy = self.assignedProxy(for: object)\n let proxy: AnyObject\n if let existingProxy = maybeProxy {\n proxy = existingProxy\n } else {\n proxy = castOrFatalError(self.createProxy(for: object))\n self.assignProxy(proxy, toObject: object)\n assert(self.assignedProxy(for: object) === proxy)\n }\n let currentDelegate = self._currentDelegate(for: object)\n let delegateProxy: Self = castOrFatalError(proxy)\n if currentDelegate !== delegateProxy {\n delegateProxy._setForwardToDelegate(currentDelegate, retainDelegate: false)\n assert(delegateProxy._forwardToDelegate() === currentDelegate)\n self._setCurrentDelegate(proxy, to: object)\n \/\/\/ 命中下面这一行assert\n assert(self._currentDelegate(for: object) === proxy)\n assert(delegateProxy._forwardToDelegate() === currentDelegate)\n }\n return delegateProxy\n }\n}"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"重点逻辑"}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"delegateProxy 即使 RxDelegateProxy"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"currentDelegate 为 RxDelegateProxy 指向的对象"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"RxDelegateProxy._setForwardToDelegate 把 RxDelegateProxy 指向真实的 Delegate"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"标红的前面一句执行的时候,是调用 setDelegate 方法,把 RxDelegateProxy 的 proxy 设置给 UIScrollView(其实是一个 UICollectionView 实例)"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"然后进入了 MySDK 的 Hook 方法,把 RxDelegateProxy 给包了一层"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"最终结果如下图"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"然后导致 self._currentDelegate(for: object) 是 DelegateProxy 而非 RxDelegateProxy,"},{"type":"text","marks":[{"type":"strong"}],"text":"触发标红断言"}]}]}]},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/infoq\/55\/55ff35af7558540f75ed9f163298dfc2.png","alt":"图片","title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"这个断言就很霸道"},{"type":"text","text":",相当于 RxCocoa 认为就只有它能够去使用 Proxy 包装 delegate,其他人不能这样做,只要做了,就断言。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"进一步分析"}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"当前状态"}]}]}]},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/infoq\/55\/55ff35af7558540f75ed9f163298dfc2.png","alt":"图片","title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"再次进入 Rx 的方法"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"currentDelegate 是 UICollectionView 指向的 DelegateProxy(MySDK 的包装)"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"delegateProxy 指向还是 RxDelegateProxy"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"触发 Rx 的 if 判断,Rx 会把其指向真实的 delegate 改向 UICollectionView 指向的 DelegateProxy"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"导致循环指向,引用链中真实的 Delegate 丢失了"}]}]}]},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/infoq\/af\/af2db4fe1a1a955949529b0ed53170c0.png","alt":"图片","title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"问题 2"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"上面提到多次调用导致了循环指向,而循环指向导致了在实际的方法转发的时候变成了死循环。"}]},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/infoq\/ca\/ca12231b269b21c442525c71acbd5687.png","alt":"图片","title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"responds 代码"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"open class RxScrollViewDelegateProxy {\n override open func responds(to aSelector: Selector!) -> Bool {\n return super.responds(to: aSelector)\n || (self._forwardToDelegate?.responds(to: aSelector) ?? false)\n || (self.voidDelegateMethodsContain(aSelector) && self.hasObservers(selector: aSelector))\n }\n}"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"@implementation BDCollectionViewDelegateProxy\n\n- (BOOL)respondsToSelector:(SEL)aSelector {\n if (aSelector == @selector(bd_isCollectionViewTrackerDecorator)) {\n return YES;\n }\n return [super respondsToSelector:aSelector];\n}\n\n@end"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"似乎只要不多次调用就没有问题了?"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"关键在于 Rx 的 setDelegate 方法也调用了 get 方法,导致一次 get 就触发第二次调用。也就是多次调用是无法避免。"}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"解决方案"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"问题的原因比较明显,如果改造 RxCocoa 的代码,把第三方可能的 Hook 考虑进来,完全可以解决问题。"}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"解决方案 1"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"参考 MySDK 的 proxy 方案,在 proxy 中加入一个特殊方法,来判断 RxDelegateProxy 是否已经在引用链中,而不去主动改变这个引用链。"}]},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/infoq\/bc\/bcd5cf5e258fe58b81974855214cf1b4.png","alt":"图片","title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"open class RxScrollViewDelegateProxy {\n public static func proxy(for object: ParentObject) -> Self {\n ...\n let currentDelegate = self._currentDelegate(for: object)\n let delegateProxy: Self = castOrFatalError(proxy)\n \/\/if currentDelegate !== delegateProxy\n if !currentDelegate.responds(to: xxxMethod) {\n delegateProxy._setForwardToDelegate(currentDelegate, retainDelegate: false)\n assert(delegateProxy._forwardToDelegate() === currentDelegate)\n self._setCurrentDelegate(proxy, to: object)\n assert(self._currentDelegate(for: object) === proxy)\n assert(delegateProxy._forwardToDelegate() === currentDelegate)\n } else {\n return currentDelegate\n }\n\n return delegateProxy\n }\n\n}\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"类似这样的改造,就可以解决问题。我们与 Rx 团队进行了沟通,也提了 PR,可惜最终被拒绝合入了。Rx 给出的说明是,Hook 是不优雅的方式,不推荐 Hook 系统的任何方法,也不想兼容任何第三方的 Hook。"}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"解决方案 2"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"有没有可能,RxCocoa 不改代码,MySDK 来兼容?"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"刚才提到,有可能是两种状态。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"状态 1"}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"setDelegate 的时候,先进 Rx 的方法,后进 MySDK 的 Hook 方法,"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"传给 Rx 的就是 delegate"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"传给 MySDK 的是 RxDelegateProxy"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Delegate 的 get 调用就触发 bug"}]}]}]},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/infoq\/5a\/5a78e89bfb906d0e45aa0971f703d68f.png","alt":"图片","title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"状态 2"}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"setDelegate 的时候,先进 MySDK 的 Hook 方法,后进 Rx 的方法?"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"传给 Rx 的就是 DelegateProxy"}]}]}]},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/infoq\/ea\/eaad6beab7ba858cd0e051063ab522a9.png","alt":"图片","title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"其实如果是状态 2,似乎 Rxcocoa 的 bug 是不会复现的。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"但是仔细查看 Rxcocoa 的 setDelegate 代码"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"extension Reactive where Base: UIScrollView {\n public func setDelegate(_ delegate: UIScrollViewDelegate)\n\n -> Disposable {\n return RxScrollViewDelegateProxy.installForwardDelegate(delegate, retainDelegate: false, onProxyForObject: self.base)\n }\n}\n\nopen class RxScrollViewDelegateProxy {\n public static func installForwardDelegate(_ forwardDelegate: Delegate, retainDelegate: Bool, onProxyForObject object: ParentObject) -> Disposable {\n weak var weakForwardDelegate: AnyObject? = forwardDelegate as AnyObject\n let proxy = self.proxy(for: object)\n assert(proxy._forwardToDelegate() === nil, \"\")\n proxy.setForwardToDelegate(forwardDelegate, retainDelegate: retainDelegate)\n return Disposables.create {\n ...\n }\n }\n}"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"emmm?Rx 里面,UICollectionView 的 setDelegate 和 Delegate 的 get 方法"},{"type":"text","marks":[{"type":"strong"}],"text":"不是 Hook..."}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"collectionView.rx.setDelegate(delegate)\n\nlet delegate = collectionView.rx.delegate"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"最终流程就只能是"}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"setDelegate 的时候,先进 Rx 的方法,传给 Rx 真实的 delegate"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"后进 MySDK 的 Hook 方法"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"传给 MySDK 的是 RxDelegateProxy"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Rx 里面获取 CollectionView 的 delegate 触发判断"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Delegate 的 get 调用就触发 bug"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果 MySDK 还是采用当前的 Hook 方案,就没法在 MySDK 解决了。"}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"解决方案 3"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"仔细看了一下,发现 Rx 里面是通过重写 RxDelegateProxy 的 forwardInvocation 来达到方法转发的目的,即"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"RxDelegateProxy 没有实现"},{"type":"codeinline","content":[{"type":"text","text":"UICollectionViewDelegate"}]},{"type":"text","text":"的任何方法"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"forwardInvocation 中处理"},{"type":"codeinline","content":[{"type":"text","text":"UICollectionViewDelegate"}]},{"type":"text","text":"相关回调"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"回顾消息转发机制"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.infoq.cn\/resource\/image\/a3\/d2\/a389945e99a7811f6430930b2c7d08d2.jpg","alt":null,"title":"","style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我们可以在 forwardingTargetForSelector 这一步进行处理,这样可以避开与 Rx 相关的冲突,处理完再直接跳过。"}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"forwardingTargetForSelector 中针对 delegate 的回调,target 返回一个 SDK 处理的类,比 DelegateProxy"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"DelegateProxy 上报完成之后,直接调用跳到 RxDelegateProxy 的 forwardInvocation 方法"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"这个解决方案其实也不完美,只能暂时规避与 Rx 的冲突。如果后续有其他 SDK 也来在这个阶段处理 Hook 冲突,也容易出现问题。"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"总结"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"确实如 Rx 团队描述的那样,Hook 不是很优雅的方式,任何 Hook 都有可能存在兼容性问题。"}]},{"type":"numberedlist","attrs":{"start":null,"normalizeStart":1},"content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"text","text":"谨慎使用 Hook"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"text","text":"Hook 系统接口一定要遵循一定的规范,不能假想只有你在 Hook 这个接口"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":3,"align":null,"origin":null},"content":[{"type":"text","text":"不要假想其他人会怎么处理,直接把多种方案集成到一起,构建多种场景,测试兼容性"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"文章列举的方案可能不全或者不完善,如果有更好的方案,欢迎讨论。"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"参考文档"}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"NSProxy 使用"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"代理模式"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"rxcocoa 学习-DelegateProxy"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"https:\/\/github.com\/ReactiveX\/RxSwift"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"本文转载自:字节跳动技术团队(ID:toutiaotechblog)"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"原文链接:"},{"type":"link","attrs":{"href":"https:\/\/mp.weixin.qq.com\/s\/8nq6q1p5-RJPiDTEJquyNQ","title":"xxx","type":null},"content":[{"type":"text","text":"无埋点核心技术:iOS Hook在字节的实践经验"}]}]}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章