0%

使用WKWebView打开itunes链接无法跳转至AppStore

UIWebView上,不需要特殊处理直接加载 itunes 链接是可以直接跳转至AppStore的。

1
2
3
4
5
6
NSURL *URL = [NSURL URLWithString:@"https://itunes.apple.com/cn/app/id1013277346?mt=8"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];

self.webView = [UIWebView new];
self.webView.delegate = self;
[self.webView loadRequest:request];

但是,在WKWebView上则不行,需要在navigationDelegate中拦截,手动openURL才能跳转至AppStore。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
- (void)loadWithWKWebView
{
NSURL *URL = [NSURL URLWithString:@"https://itunes.apple.com/cn/app/id1013277346?mt=8"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];

self.wkWebView = [WKWebView new];
self.wkWebView.navigationDelegate = self;
[self.wkWebView loadRequest:request];
}

- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler
{
WKNavigationActionPolicy policy = WKNavigationActionPolicyAllow;
/* 简单判断host,真实App代码中,需要更精确判断itunes链接 */
if ([[navigationAction.request.URL host] isEqualToString:@"itunes.apple.com"] &&
[[UIApplication sharedApplication] openURL:navigationAction.request.URL])
{
policy = WKNavigationActionPolicyCancel;

}

decisionHandler(policy);
}

难以理解为啥WKWebView上不支持跳转至AppStore。
如果不处理,普通网页上链接至itunes的,点击后都无法跳转了。