WKWebView 如何在 WKNavigationDelegate 方法中找到请求
WKWebView how to find the request in the WKNavigationDelegate methods
我正在尝试从 UIWebView 迁移到 WKWebView。 shouldStartLoadWithRequest 委托方法我可以找到如下请求:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSLog(@"request: %@", request);
if ([request.URL.host isEqualToString:@"jsbridge"])
{
NSString* requestType = [request.URL.pathComponents objectAtIndex:2];
NSString* key = [request.URL.pathComponents objectAtIndex:3];
NSLog(@"requestType: %@ - key: %@", requestType, key);
if([requestType isEqualToString:@"audio"])
{
[self playAudio:key];
}
return YES;
}
我需要帮助在 WKNavigationDelegate 方法中找到相同的请求。我是否使用 didCommit 或 didFinishNavigation 方法,如果是,我如何找到它来自的请求?有人可以给我举个例子吗?希望我能做到这一点是 Swift 但我必须坚持 Objective-C。提前致谢。
据我所知,UIWebView
中的 webView:shouldStartLoadWithRequest:navigationType:
相当于 WKWebView
中的 webView:decidePolicyForNavigationAction:decisionHandler:
。
在 webView:decidePolicyForNavigationAction:decisionHandler:
中,您可以从 navigationAction
获取请求。
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
NSURLRequest *request = navigationAction.request;
// Do whatever you want with the request
decisionHandler(WKNavigationActionPolicyAllow);
}
UIWebview 等价于 WKWebview 如下:
didFailLoadWithError => didFailNavigation
webViewDidFinishLoad => didFinishNavigation
webViewDidStartLoad => didStartProvisionalNavigation
shouldStartLoadWithRequest => decidePolicyForNavigationAction
WKWebview(Swift版)中的shouldStartWithRequest委托方法如下:
func webView(webView: WKWebView, decidePolicyForNavigationAction navigationAction: WKNavigationAction, decisionHandler: ((WKNavigationActionPolicy) -> Void)) {
if (webView.url?.host ?? "") == "jsbridge" {
let requestType = webView.url?.pathComponents[2]
let key = webView.url?.pathComponents[3]
print("requestType: \(requestType ?? "") - key: \(key ?? "")")
if (requestType == "audio") {
playAudio(key)
}
}
}
我创建了自定义 class
class PKWebKitView: WKWebView,WKUIDelegate,WKNavigationDelegate {
var webView :WKWebView!
func loadRequestUrl(myRequest : URLRequest) {
self.navigationDelegate = self
self.uiDelegate = self
self.load(myRequest)
} }
extension PKWebKitView: WKNavigationDelegate {
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
if let host = navigationAction.request.url?.absoluteString.removingPercentEncoding {
if host.contains(<endpoint check>) {
if let delegateWrapper = wkWebviewDelegate {
decisionHandler(.cancel)
}
}
if host.contains(<endpoint check>) {
let responseString = host
let query = "<Your query>"
wkWebviewDelegate?.launchScreen(query)
}
}
使用:
let webConfiguration = WKWebViewConfiguration()
webView = PKWebKitView(frame: webViewContainer.frame, configuration: webConfiguration)
webView.loadRequestUrl(myRequest
我正在尝试从 UIWebView 迁移到 WKWebView。 shouldStartLoadWithRequest 委托方法我可以找到如下请求:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSLog(@"request: %@", request);
if ([request.URL.host isEqualToString:@"jsbridge"])
{
NSString* requestType = [request.URL.pathComponents objectAtIndex:2];
NSString* key = [request.URL.pathComponents objectAtIndex:3];
NSLog(@"requestType: %@ - key: %@", requestType, key);
if([requestType isEqualToString:@"audio"])
{
[self playAudio:key];
}
return YES;
}
我需要帮助在 WKNavigationDelegate 方法中找到相同的请求。我是否使用 didCommit 或 didFinishNavigation 方法,如果是,我如何找到它来自的请求?有人可以给我举个例子吗?希望我能做到这一点是 Swift 但我必须坚持 Objective-C。提前致谢。
据我所知,UIWebView
中的 webView:shouldStartLoadWithRequest:navigationType:
相当于 WKWebView
中的 webView:decidePolicyForNavigationAction:decisionHandler:
。
在 webView:decidePolicyForNavigationAction:decisionHandler:
中,您可以从 navigationAction
获取请求。
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
NSURLRequest *request = navigationAction.request;
// Do whatever you want with the request
decisionHandler(WKNavigationActionPolicyAllow);
}
UIWebview 等价于 WKWebview 如下:
didFailLoadWithError => didFailNavigation
webViewDidFinishLoad => didFinishNavigation
webViewDidStartLoad => didStartProvisionalNavigation
shouldStartLoadWithRequest => decidePolicyForNavigationAction
WKWebview(Swift版)中的shouldStartWithRequest委托方法如下:
func webView(webView: WKWebView, decidePolicyForNavigationAction navigationAction: WKNavigationAction, decisionHandler: ((WKNavigationActionPolicy) -> Void)) {
if (webView.url?.host ?? "") == "jsbridge" {
let requestType = webView.url?.pathComponents[2]
let key = webView.url?.pathComponents[3]
print("requestType: \(requestType ?? "") - key: \(key ?? "")")
if (requestType == "audio") {
playAudio(key)
}
}
}
我创建了自定义 class
class PKWebKitView: WKWebView,WKUIDelegate,WKNavigationDelegate {
var webView :WKWebView!
func loadRequestUrl(myRequest : URLRequest) {
self.navigationDelegate = self
self.uiDelegate = self
self.load(myRequest)
} }
extension PKWebKitView: WKNavigationDelegate {
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
if let host = navigationAction.request.url?.absoluteString.removingPercentEncoding {
if host.contains(<endpoint check>) {
if let delegateWrapper = wkWebviewDelegate {
decisionHandler(.cancel)
}
}
if host.contains(<endpoint check>) {
let responseString = host
let query = "<Your query>"
wkWebviewDelegate?.launchScreen(query)
}
}
使用:
let webConfiguration = WKWebViewConfiguration()
webView = PKWebKitView(frame: webViewContainer.frame, configuration: webConfiguration)
webView.loadRequestUrl(myRequest