dreadbot
dreadbot

Reputation: 962

WKWebView how to find the request in the WKNavigationDelegate methods

I'm trying to migrate from a UIWebView to WKWebView. shouldStartLoadWithRequest delegate method I could find the request as below:

- (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;
}

I need help finding the same request in the WKNavigationDelegate methods. Do I use the didCommit or didFinishNavigation methods and if so how do I find the request that it came from? Could someone give me an example? Wish I could do this is Swift but I have to stick to Objective-C. Thanks in advance.

Upvotes: 0

Views: 3518

Answers (3)

Praveen-K
Praveen-K

Reputation: 3401

I created the custom 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)
            }
}

To Use:

let webConfiguration = WKWebViewConfiguration()
webView = PKWebKitView(frame: webViewContainer.frame, configuration: webConfiguration)
webView.loadRequestUrl(myRequest

Upvotes: 1

Gowri K
Gowri K

Reputation: 55

Equivalent for UIWebview to WKWebview is as follows:

didFailLoadWithError => didFailNavigation
webViewDidFinishLoad => didFinishNavigation
webViewDidStartLoad => didStartProvisionalNavigation
shouldStartLoadWithRequest => decidePolicyForNavigationAction

The shouldStartWithRequest delegate method in WKWebview (Swift version) is as follows:

    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)
            }
        }
    }

Upvotes: 0

trungduc
trungduc

Reputation: 12154

As I know the equivalent of webView:shouldStartLoadWithRequest:navigationType: in UIWebView is webView:decidePolicyForNavigationAction:decisionHandler: in WKWebView.

Inside webView:decidePolicyForNavigationAction:decisionHandler:, you can get the request from 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);
}

Upvotes: 3

Related Questions