ChrisOSX
ChrisOSX

Reputation: 744

WKWebView won't open external links

So I was in the process of converting my UIWebView app over to WKWebView only to find out that it won't open external websites, i.e. dropbox, facebook etc.

It loads my site in the viewDidLoad, so that's not an issue.

example:

NSURL *nsurl=[NSURL URLWithString:@"example.com"];
NSURLRequest *nsrequest=[NSURLRequest requestWithURL:nsurl];
[webView loadRequest:nsrequest];

webView.navigationDelegate = self;
webView.UIDelegate = self;
[self.view addSubview:webView];

I have called:

-(void)webView:(WKWebView *)webView didStartProvisionalNavigation: (WKNavigation *)navigation {}

- (void)webView:(WKWebView *)webView didFinishNavigation: (WKNavigation *)navigation{}

along with:

- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {}

decisionHandler(WKNavigationActionPolicyAllow);

and finally in my info.plist I added:

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
    <key>NSExceptionAllowsInsecureHTTPLoads</key>
    <true/>
    <key>NSIncludesSubdomains</key>
    <true/>
    <key>NSThirdPartyExceptionAllowsInsecureHTTPLoads</key>
    <true/>
</dict>

Yet nothing works. What am I doing wrong?

Any help would as always be greatly appreciated.

Upvotes: 9

Views: 14871

Answers (3)

shashi shekhar
shashi shekhar

Reputation: 11

Here is the swift 3.0 version of this answer Add these two delegates

// webView is WKWebview object  
 webView.navigationDelegate = self;
 webView.uiDelegate = self;

override these delegate callback methods

  func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation) {
        UIApplication.shared.isNetworkActivityIndicatorVisible = true
        print("didStartProvisionalNavigation: \(navigation)")
    }

func webView(_ webView: WKWebView, didReceiveServerRedirectForProvisionalNavigation navigation: WKNavigation) {
        print("didReceiveServerRedirectForProvisionalNavigation: \(navigation)")
    }

    func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation, withError error: Error) {
        print("didFailProvisionalNavigation: \(navigation), error: \(error)")
    }

func webView(_ webView: WKWebView, didFinishLoading navigation: WKNavigation) {
        UIApplication.shared.isNetworkActivityIndicatorVisible = false
        print("didFinishLoadingNavigation: \(navigation)")
    }


    func _webViewWebProcessDidCrash(_ webView: WKWebView) {
        print("WebContent process crashed; reloading")
    }

func webView(_ webView: WKWebView,createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView?{
        //if <a> tag does not contain attribute or has _blank then navigationAction.targetFrame will return nil
        if let trgFrm = navigationAction.targetFrame {

            if(!trgFrm.isMainFrame){
                UIApplication.shared.isNetworkActivityIndicatorVisible = true
                self.webView.load(navigationAction.request)
            }
        }



    return nil
}

func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!){

    UIApplication.shared.isNetworkActivityIndicatorVisible = false
    print("didFinish: \(String(describing: self.webView.url)); stillLoading:\(self.webView.isLoading ? "NO" : "YES")")

}



func webView(_ webView: WKWebView, decidePolicyFor navigationResponse: WKNavigationResponse, decisionHandler: @escaping (_: WKNavigationResponsePolicy) -> Void) {
    print("decidePolicyForNavigationResponse")
    decisionHandler(.allow)
}

func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (_: WKNavigationActionPolicy) -> Void) {

     decisionHandler(.allow)
}

Upvotes: 1

Jeba Moses
Jeba Moses

Reputation: 849

Pease check the return type of this method,

- (WKWebView *)webView:(WKWebView *)webView createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration forNavigationAction:(WKNavigationAction *)navigationAction windowFeatures:(WKWindowFeatures *)windowFeatures {

        if (!navigationAction.targetFrame.isMainFrame) {

            [self.webView loadRequest:navigationAction.request];
        }
        return self.webView;
    }

As it is WKWebView, if you return your webview, it will load the URL for you and give you the desired page to be displayed.

Upvotes: 0

ChrisOSX
ChrisOSX

Reputation: 744

Ok So I managed to get it, along with some help from some forgotten sites (sorry) I managed to get it all working.

So below is pretty much everything I'm using in my webview.

//Webview Start/Finish Request
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation {
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    NSLog(@"didStartProvisionalNavigation: %@", navigation);
}

- (void)webView:(WKWebView *)webView didReceiveServerRedirectForProvisionalNavigation:(WKNavigation *)navigation {
    NSLog(@"didReceiveServerRedirectForProvisionalNavigation: %@", navigation);
}

- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation withError:(NSError *)error {
    NSLog(@"didFailProvisionalNavigation: %@navigation, error: %@", navigation, error);
}

- (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation {
    NSLog(@"didCommitNavigation: %@", navigation);
}

- (void)webView:(WKWebView *)webView didFinishLoadingNavigation:(WKNavigation *)navigation {
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    NSLog(@"didFinishLoadingNavigation: %@", navigation);
}

- (void)webView:(WKWebView *)webView didFailNavigation:(WKNavigation *)navigation withError:(NSError *)error {
    NSLog(@"didFailNavigation: %@, error %@", navigation, error);
}

- (void)_webViewWebProcessDidCrash:(WKWebView *)webView {
    NSLog(@"WebContent process crashed; reloading");
}

- (WKWebView *)webView:(WKWebView *)webView createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration forNavigationAction:(WKNavigationAction *)navigationAction windowFeatures:(WKWindowFeatures *)windowFeatures {

    if (!navigationAction.targetFrame.isMainFrame) {
        [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
        [self.webView loadRequest:navigationAction.request];
    }
    return nil;
}

- (void)webView:(WKWebView *)webView didFinishNavigation: (WKNavigation *)navigation{
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    NSLog(@"didFinish: %@; stillLoading:%@", [self.webView URL], (self.webView.loading?@"NO":@"YES"));
}

- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler {
    NSLog(@"decidePolicyForNavigationResponse");
    decisionHandler(WKNavigationResponsePolicyAllow);
}

- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {

    if (decisionHandler) {
        decisionHandler(WKNavigationActionPolicyAllow);
    }
}

So I help all of this helps somebody.

Upvotes: 16

Related Questions