Itunes应用程序链接不适用于WkWebview

我加载到Wkwebview的网页之一具有以下iTunes应用程序链接

 https://itunes.apple.com/gb/app/xx-yy-zz/id435919263?mt=8

打开后,我会收到以下警报

Itunes应用程序链接不适用于WkWebview

这是我遇到的错误。

{
    "[errorCode]" = 0;
    "[errorDescription]" = "Redirection to URL with a scheme that is not HTTP(S)";
    "[errordetail]" = "Con:myappxxxx:myorder:webview:networkerror";
    "[localizedRecoverySuggestion]" = "";
    "[url]" = "itms-appss://apps.apple.com/gb/app/xx-yy-zz/id435919263";
}

当使用相同的iTunes链接(https://itunes.apple.com/gb/app/xx-yy-zz/id435919263?mt=8)  在UIWebview中打开,我看到URL被重定向到以下URL并在appstore中打开了应用

 itms-appss://itunes.apple.com/gb/app/xx-yy-zz/id435919263?mt=8

在Wkwebview中,URL被重定向到后续URL

 itms-appss://apps.apple.com/gb/app/xx-yy-zz/id435919263

感谢您的帮助


更新

为了运输安全,我什至尝试了任意上传,但问题仍然存在。

  

Error Domain = Code = 0“使用非方案重定向到URL   HTTP(S)”   UserInfo = {__ WKRecoveryAttempterErrorKey =,   NSErrorFailingURLStringKey = itms-appss://apps.apple.com/gb/app/xx-yy-zz/id435919263,   NSErrorFailingURLKey = itms-appss://apps.apple.com/gb/app/xx-yy-zz/id435919263,   NSLocalizedDescription =使用非方案重定向到URL   HTTP(S)}

haishizhende 回答:Itunes应用程序链接不适用于WkWebview

我认为您可以尝试在wkwebview的委托方法中拦截itunes链接,并使用openURL打开URL

下面的源代码将在wkwebview中打开所有itms-appss链接。不要忘记遵守 WKNavigationDelegate

   - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
    if ([webURL.scheme isEqualToString:@"itms-appss"])
        {
                UIApplication *app = [UIApplication sharedApplication];
    if ([app canOpenURL:webURL])
    {
        [self.webviewObj stopLoading];
        [app openURL:[NSURL URLWithString:[webURL absoluteString]]];
        decisionHandler(WKNavigationActionPolicyCancel);
     } else{
        decisionHandler(WKNavigationActionPolicyCancel);
       }
        }
    else
       {
            decisionHandler(WKNavigationActionPolicyAllow);
        }
     return;
    }
,

WKWebView在默认情况下似乎不处理非http的URL模式。 因此,您必须使用webView(_:decidePolicyFor:decisionHandler:)捕获请求,并检查WKWebView可以加载的URL。 如果该URL是非http的URL,则应通过open(_:options:completionHandler:)打开该URL。

这里是示例代码。

为您的WKWebView实例分配navigationDelegate。

webView.navigationDelegate = self

实施WKNavigationDelegate方法。

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

    // if the url is not http(s) schema,then the UIApplication open the url
    if let url = navigationAction.request.url,!url.absoluteString.hasPrefix("http://"),!url.absoluteString.hasPrefix("https://"),UIApplication.shared.canOpenURL(url) {

        UIApplication.shared.open(url,options: [:],completionHandler: nil)
        // cancel the request
        decisionHandler(.cancel)
    } else {
        // allow the request
        decisionHandler(.allow)
    }
}
,

尝试在您的info.plist中添加:

应用传输安全性设置   -允许任意载荷=是

<key>NSAppTransportSecurity</key>
<dict>
    <!--Connect to anything (this is probably BAD)-->
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>
本文链接:https://www.f2er.com/3145227.html

大家都在问