WKWebView 不会打开外部链接

Posted

技术标签:

【中文标题】WKWebView 不会打开外部链接【英文标题】:WKWebView won't open external links 【发布时间】:2016-03-28 05:05:15 【问题描述】:

所以我正在将我的 UIWebView 应用程序转换为 WKWebView 只是发现它不会打开外部网站,即 dropbox、facebook 等。

它会在 viewDidLoad 中加载我的网站,所以这不是问题。

示例:

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

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

我打过电话:

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

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

还有:

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

decisionHandler(WKNavigationActionPolicyAllow);

最后在我的 info.plist 中我添加了:

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

但没有任何效果。我做错了什么?

我们将一如既往地为您提供任何帮助。

【问题讨论】:

【参考方案1】:

好的,所以我设法得到了它,以及一些被遗忘的网站的帮助(抱歉),我设法让这一切正常工作。

下面是我在 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);
    

所以我帮助所有这些帮助某人。

【讨论】:

【参考方案2】:

这是这个答案的 swift 3.0 版本 添加这两个代表

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

覆盖这些委托回调方法

  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)

【讨论】:

【参考方案3】:

请检查此方法的返回类型,

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

因为它是 WKWebView,如果你返回你的 webview,它会为你加载 URL 并给你想要显示的页面。

【讨论】:

以上是关于WKWebView 不会打开外部链接的主要内容,如果未能解决你的问题,请参考以下文章

无法使用 WKWebView 在 Safari 中打开外部链接

当在 WKWebView 中加载链接时,我如何知道外部应用程序是不是会打开?

UIWebView 和 WKWebView 导航

WKWebview 在 macOS 上的默认浏览器中打开目标 =“_blank”链接

微信小程序可不可以访问外部链接

Cordova 应用程序外部链接在 iOS 上不起作用