Itunes 应用程序链接不适用于 WkWebview
Itunes app links are not working with WkWebview
我加载到 Wkwebview 的网页之一有以下 iTunes 应用程序link
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";
}
当同一个 iTunes link ( 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
感谢任何帮助
更新
我什至尝试将任意上传设置为 true 以确保传输安全,但问题仍然存在。
Error Domain= Code=0 "Redirection to URL with a scheme that is not
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=Redirection to URL with a scheme that is not
HTTP(S)}
尝试在您的 info.plist 中添加:
应用传输安全设置
-- 允许任意加载 = yes
<key>NSAppTransportSecurity</key>
<dict>
<!--Connect to anything (this is probably BAD)-->
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
我认为你可以尝试在 wkwebview 的委托方法中拦截 itunes link 并使用 openURL
打开 URL
下面的源代码将在 wkwebview 中打开任何 itms-appss links。不要忘记遵守 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(s) url 模式 。
因此,您必须使用 webView(_:decidePolicyFor:decisionHandler:)
捕获请求,并检查可以由 WKWebView
加载的 url。
如果 url 是非 http(s) 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)
}
}
我加载到 Wkwebview 的网页之一有以下 iTunes 应用程序link
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";
}
当同一个 iTunes link ( 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
感谢任何帮助
更新
我什至尝试将任意上传设置为 true 以确保传输安全,但问题仍然存在。
Error Domain= Code=0 "Redirection to URL with a scheme that is not 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=Redirection to URL with a scheme that is not HTTP(S)}
尝试在您的 info.plist 中添加:
应用传输安全设置 -- 允许任意加载 = yes
<key>NSAppTransportSecurity</key>
<dict>
<!--Connect to anything (this is probably BAD)-->
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
我认为你可以尝试在 wkwebview 的委托方法中拦截 itunes link 并使用 openURL
打开 URL下面的源代码将在 wkwebview 中打开任何 itms-appss links。不要忘记遵守 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(s) url 模式 。
因此,您必须使用 webView(_:decidePolicyFor:decisionHandler:)
捕获请求,并检查可以由 WKWebView
加载的 url。
如果 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)
}
}