forked from illusionspaces/WKJavaScriptBridge
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWKWebViewDelegate.m
More file actions
executable file
·119 lines (92 loc) · 5.6 KB
/
WKWebViewDelegate.m
File metadata and controls
executable file
·119 lines (92 loc) · 5.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
//
// WKWebViewDelegate.m
// Hybrid-framework
//
// Created by 王凯 on 2018/12/6.
// Copyright © 2018 王凯. All rights reserved.
//
#import "WKWebViewDelegate.h"
#import "WKWebViewEngine.h"
#define WVJB_WKWEBVIEW_DELEGATE_TYPE NSObject<WKNavigationDelegate>
@implementation WKWebViewDelegate {
__weak WKWebViewEngine *_webViewEngine;
}
- (instancetype)initWithWebViewEngine:(WKWebViewEngine *)webViewEngine {
if (self = [super init]) {
_webViewEngine = webViewEngine;
}
return self;
}
#pragma mark - WKNavigationDelegate
// 决定导航的动作,通常用于处理跨域的链接能否导航。
// WebKit对跨域进行了安全检查限制,不允许跨域,因此我们要对不能跨域的链接单独处理。
// 但是,对于Safari是允许跨域的,不用这么处理。
// 这个是决定是否Request
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler{
__strong WVJB_WKWEBVIEW_DELEGATE_TYPE* strongDelegate = _webViewEngine.webViewDelegate;
if (strongDelegate && [strongDelegate respondsToSelector:@selector(webView:decidePolicyForNavigationAction:decisionHandler:)]) {
[_webViewEngine.webViewDelegate webView:webView decidePolicyForNavigationAction:navigationAction decisionHandler:decisionHandler];
}else {
decisionHandler(WKNavigationActionPolicyAllow);
}
}
- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler {
__strong WVJB_WKWEBVIEW_DELEGATE_TYPE* strongDelegate = _webViewEngine.webViewDelegate;
if (strongDelegate && [strongDelegate respondsToSelector:@selector(webView:decidePolicyForNavigationResponse:decisionHandler:)]) {
[strongDelegate webView:webView decidePolicyForNavigationResponse:navigationResponse decisionHandler:decisionHandler];
}else {
decisionHandler(WKNavigationResponsePolicyAllow);
}
}
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(null_unspecified WKNavigation *)navigation {
__strong WVJB_WKWEBVIEW_DELEGATE_TYPE* strongDelegate = _webViewEngine.webViewDelegate;
if (strongDelegate && [strongDelegate respondsToSelector:@selector(webView:didStartProvisionalNavigation:)]) {
[strongDelegate webView:webView didStartProvisionalNavigation:navigation];
}
}
- (void)webView:(WKWebView *)webView didReceiveServerRedirectForProvisionalNavigation:(null_unspecified WKNavigation *)navigation {
__strong WVJB_WKWEBVIEW_DELEGATE_TYPE* strongDelegate = _webViewEngine.webViewDelegate;
if (strongDelegate && [strongDelegate respondsToSelector:@selector(webView:didReceiveServerRedirectForProvisionalNavigation:)]) {
[strongDelegate webView:webView didReceiveServerRedirectForProvisionalNavigation:navigation];
}
}
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error {
__strong WVJB_WKWEBVIEW_DELEGATE_TYPE* strongDelegate = _webViewEngine.webViewDelegate;
if (strongDelegate && [strongDelegate respondsToSelector:@selector(webView:didFailProvisionalNavigation:withError:)]) {
[strongDelegate webView:webView didFailProvisionalNavigation:navigation withError:error];
}
}
- (void)webView:(WKWebView *)webView didCommitNavigation:(null_unspecified WKNavigation *)navigation {
__strong WVJB_WKWEBVIEW_DELEGATE_TYPE* strongDelegate = _webViewEngine.webViewDelegate;
if (strongDelegate && [strongDelegate respondsToSelector:@selector(webView:didCommitNavigation:)]) {
[strongDelegate webView:webView didCommitNavigation:navigation];
}
}
- (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation {
__strong WVJB_WKWEBVIEW_DELEGATE_TYPE* strongDelegate = _webViewEngine.webViewDelegate;
if (strongDelegate && [strongDelegate respondsToSelector:@selector(webView:didFinishNavigation:)]) {
[strongDelegate webView:webView didFinishNavigation:navigation];
}
}
- (void)webView:(WKWebView *)webView didFailNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error {
__strong WVJB_WKWEBVIEW_DELEGATE_TYPE* strongDelegate = _webViewEngine.webViewDelegate;
if (strongDelegate && [strongDelegate respondsToSelector:@selector(webView:didFailNavigation:withError:)]) {
[strongDelegate webView:webView didFailNavigation:navigation withError:error];
}
}
- (void)webView:(WKWebView *)webView didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential * _Nullable credential))completionHandler {
__strong WVJB_WKWEBVIEW_DELEGATE_TYPE* strongDelegate = _webViewEngine.webViewDelegate;
if (strongDelegate && [strongDelegate respondsToSelector:@selector(webView:didReceiveAuthenticationChallenge:completionHandler:)]) {
[strongDelegate webView:webView didReceiveAuthenticationChallenge:challenge completionHandler:completionHandler];
}
}
- (void)webViewWebContentProcessDidTerminate:(WKWebView *)webView {
__strong WVJB_WKWEBVIEW_DELEGATE_TYPE* strongDelegate = _webViewEngine.webViewDelegate;
if (strongDelegate && [strongDelegate respondsToSelector:@selector(webViewWebContentProcessDidTerminate:)]) {
if (@available(iOS 9.0, *)) {
[strongDelegate webViewWebContentProcessDidTerminate:webView];
} else {
}
}
}
@end