NSURLProtocol对WKWebView的处理

.h

#import <Foundation/Foundation.h>



NS_ASSUME_NONNULL_BEGIN



FOUNDATION_EXTERN NSString *const HTTP_KEY;

FOUNDATION_EXTERN NSString *const HTTPS_KEY;



@interface NSURLProtocol (CSWebKitSupport)



+ (void)wk_registerScheme:(NSString*)scheme;

+ (void)wk_unregisterScheme:(NSString*)scheme;



@end



NS_ASSUME_NONNULL_END

 

.m

#import "NSURLProtocol+CSWebKitSupport.h"

#import <WebKit/WebKit.h>



NSString *const HTTP_KEY = @"http";

NSString *const HTTPS_KEY = @"https";



FOUNDATION_STATIC_INLINE Class ContextControllerClass() {

    static Class cls;

    if (!cls) {

        cls = [[[WKWebView new] valueForKey:@"browsingContextController"] class];

    }

    return cls;

}



FOUNDATION_STATIC_INLINE SEL RegisterSchemeSelector() {

    return NSSelectorFromString(@"registerSchemeForCustomProtocol:");

}



FOUNDATION_STATIC_INLINE SEL UnregisterSchemeSelector() {

    return NSSelectorFromString(@"unregisterSchemeForCustomProtocol:");

}



@implementation NSURLProtocol (CSWebKitSupport)



+ (void)wk_registerScheme:(NSString *)scheme {

    Class cls = ContextControllerClass();

    SEL sel = RegisterSchemeSelector();

    if ([(id)cls respondsToSelector:sel]) {

#pragma clang diagnostic push

#pragma clang diagnostic ignored "-Warc-performSelector-leaks"

        [(id)cls performSelector:sel withObject:scheme];

#pragma clang diagnostic pop

    }

}



+ (void)wk_unregisterScheme:(NSString *)scheme {

    Class cls = ContextControllerClass();

    SEL sel = UnregisterSchemeSelector();

    if ([(id)cls respondsToSelector:sel]) {

#pragma clang diagnostic push

#pragma clang diagnostic ignored "-Warc-performSelector-leaks"

        [(id)cls performSelector:sel withObject:scheme];

#pragma clang diagnostic pop

    }

}



@end

 

上一篇:UE4 You cannot use raw method delegates with UObjects


下一篇:mininet + opendaylight环境配置