其他分享
首页 > 其他分享> > NSURLProtocol对WKWebView的处理

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

 

标签:scheme,NSURLProtocol,处理,clang,NSString,pragma,sel,WKWebView,cls
来源: https://blog.csdn.net/Tony_Hsu/article/details/106809056