先看代码:
+ (void)_registerDefaultHandlers { [self registerHandler:[WXResourceRequestHandlerDefaultImpl new] withProtocol:@protocol(WXResourceRequestHandler)]; [self registerHandler:[WXNavigationDefaultImpl new] withProtocol:@protocol(WXNavigationProtocol)]; [self registerHandler:[WXURLRewriteDefaultImpl new] withProtocol:@protocol(WXURLRewriteProtocol)]; }
WXSDKEngine中默认注册了3个Handler。
+ (void)registerHandler:(id)handler withProtocol:(Protocol *)protocol { WXAssert(handler && protocol, @"Fail to register the handler, please check if the parameters are correct !"); [WXHandlerFactory registerHandler:handler withProtocol:protocol]; }
WXSDKEngine会继续调用WXHandlerFactory的registerHandler:withProtocol:方法。
@interface WXHandlerFactory : NSObject @property (nonatomic, strong) WXThreadSafeMutableDictionary *handlers; + (void)registerHandler:(id)handler withProtocol:(Protocol *)protocol; + (id)handlerForProtocol:(Protocol *)protocol; + (NSDictionary *)handlerConfigs; @end
WXHandlerFactory也是一个单例,里面有一个线程安全的字典handlers,用来保存实例和Protocol名的映射表。