ios为什么我不能钩住WKCustomProtocol方法

时间:2018-12-17 17:48:20

标签: ios iphone runtime

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

breakpoint screenshots

0 个答案:

没有答案