7

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

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

当它打开时,我收到以下警报

在此处输入图像描述

这是我遇到的错误。

{
    "[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";
}

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

 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

任何帮助表示赞赏


更新

我什至尝试将任意上传设置为 true 以确保传输安全,但问题仍然存在。

错误域 = 代码 = 0“使用不是 HTTP(S) 的方案重定向到 URL”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=使用非 HTTP(S) 方案重定向到 URL}

4

3 回答 3

9

我认为您可以尝试在 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;
    }
于 2019-11-13T11:00:30.770 回答
4

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

这里是示例代码。

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)
    }
}
于 2019-11-15T03:25:56.223 回答
0

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

应用程序传输安全设置 -- 允许任意加载 = 是

<key>NSAppTransportSecurity</key>
<dict>
    <!--Connect to anything (this is probably BAD)-->
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>
于 2019-11-08T01:22:49.197 回答