WKCustomProtocol是WebKit source code的类形式,我已经调试了它。它存在
HDWebURLProtocol,WKCustomProtocol超类是NSURLProtocol。我钩上了自己的类“ HDWebURLProtocol”,该类已成功,但是WKCustomProtocol不成功;
我使用Xcode 10.1,iOS 12.1模拟器,调试运行WebKit code
这是我的代码:
+ (void)exchange {
Class WKCustomProtocolClass = object_getClass(NSClassFromString(@"WKCustomProtocol")); // HDWebURLProtocol
SEL canInitWithRequest = NSSelectorFromString(@"canInitWithRequest:");
SEL af_canInitWithRequest = NSSelectorFromString(@"fix_canInitWithRequest:");
BOOL add1 = class_addMethod(WKCustomProtocolClass,
canInitWithRequest,
class_getMethodImplementation(WKCustomProtocolClass, canInitWithRequest),
"B@:@");
BOOL add2 = class_addMethod(WKCustomProtocolClass,
af_canInitWithRequest,
(IMP)af_canInitWithRequest2,
"B@:@");
NSLog(@"%d,%d", add1, add2);
Method orgi = class_getClassMethod(WKCustomProtocolClass, canInitWithRequest);
Method after = class_getClassMethod(WKCustomProtocolClass, af_canInitWithRequest);
method_exchangeImplementations(orgi, after);
}
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
BOOL af_canInitWithRequest2(id sender, SEL cmd, NSURLRequest *req) {
SEL canInitWithRequest = NSSelectorFromString(@"fix_canInitWithRequest:");
return [sender performSelector:canInitWithRequest withObject:req];
}
#pragma clang diagnostic pop