Skip to content

Commit

Permalink
补充KKJSBridge/AjaxHook方案下的资源拦截demo
Browse files Browse the repository at this point in the history
  • Loading branch information
karosLi committed Jan 12, 2021
1 parent 490af42 commit 9797e96
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 1 deletion.
6 changes: 6 additions & 0 deletions KKJSBridge.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ Pod::Spec.new do |s|
sub.source_files = "KKJSBridge/KKJSBridge/Modules/Ajax/AjaxHook/**/*.{h,m}"
sub.resources = "KKJSBridge/KKJSBridge/JS/KKJSBridgeAJAXHook.js"
sub.dependency "KKJSBridge/Core"
sub.user_target_xcconfig = {
'GCC_PREPROCESSOR_DEFINITIONS' => '$(inherited) KKAjaxHook=1'
}
sub.pod_target_xcconfig = {
'GCC_PREPROCESSOR_DEFINITIONS' => 'KKAjaxHook=1'
}
Expand All @@ -31,6 +34,9 @@ Pod::Spec.new do |s|
sub.source_files = "KKJSBridge/KKJSBridge/Modules/Ajax/AjaxProtocolHook/**/*.{h,m}"
sub.resources = "KKJSBridge/KKJSBridge/JS/KKJSBridgeAJAXProtocolHook.js"
sub.dependency "KKJSBridge/Core"
sub.user_target_xcconfig = {
'GCC_PREPROCESSOR_DEFINITIONS' => '$(inherited) KKAjaxProtocolHook=1'
}
sub.pod_target_xcconfig = {
'GCC_PREPROCESSOR_DEFINITIONS' => 'KKAjaxProtocolHook=1'
}
Expand Down
3 changes: 2 additions & 1 deletion KKJSBridgeDemo/KKJSBridgeDemo/HtmlURLProtocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
NS_ASSUME_NONNULL_BEGIN

@interface HtmlURLProtocol : NSURLProtocol

+ (void)HtmlURLProtocolRegisterScheme:(NSString *)scheme;
+ (void)HtmlURLProtocolUnregisterScheme:(NSString *)scheme;
@end

NS_ASSUME_NONNULL_END
46 changes: 46 additions & 0 deletions KKJSBridgeDemo/KKJSBridgeDemo/HtmlURLProtocol.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,28 @@
//

#import "HtmlURLProtocol.h"
#import <WebKit/WebKit.h>

// https://github.com/WebKit/webkit/blob/989f1ffc97f6b168687cbfc6f98d35880fdd29de/Source/WebKit/UIProcess/API/Cocoa/WKBrowsingContextController.mm
Class HtmlURLProtocol_WKWebView_ContextControllerClass() {
static Class cls;
if (!cls) {
if (@available(iOS 8.0, *)) {
cls = [[[WKWebView new] valueForKey:@"browsingContextController"] class];
} else {

}
}
return cls;
}
//customSchemes
SEL HtmlURLProtocol_WKWebView_RegisterSchemeSelector() {
return NSSelectorFromString(@"registerSchemeForCustomProtocol:");
}

SEL HtmlURLProtocol_WKWebView_UnregisterSchemeSelector() {
return NSSelectorFromString(@"unregisterSchemeForCustomProtocol:");
}

static NSString * const kKKJSBridgeNSURLProtocolKey = @"kKKJSBridgeNSURLProtocolKey1";

Expand Down Expand Up @@ -82,4 +104,28 @@ - (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)data
[self.client URLProtocol:self didLoadData:data];
}

#pragma mark - 协议注册

+ (void)HtmlURLProtocolRegisterScheme:(NSString *)scheme {
Class cls = HtmlURLProtocol_WKWebView_ContextControllerClass();
SEL sel = HtmlURLProtocol_WKWebView_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)HtmlURLProtocolUnregisterScheme:(NSString *)scheme {
Class cls = HtmlURLProtocol_WKWebView_ContextControllerClass();
SEL sel = HtmlURLProtocol_WKWebView_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
4 changes: 4 additions & 0 deletions KKJSBridgeDemo/KKJSBridgeDemo/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ - (void)viewDidLoad {
NSLog(@"libraryPath = %@", libraryPath);

// 注册 HtmlURLProtocol,使用离线包
#ifndef KKAjaxProtocolHook
[HtmlURLProtocol HtmlURLProtocolRegisterScheme:@"http"];
[HtmlURLProtocol HtmlURLProtocolRegisterScheme:@"https"];
#endif
[NSURLProtocol registerClass:HtmlURLProtocol.class];
}

Expand Down

0 comments on commit 9797e96

Please sign in to comment.