Itunes app links are not working with WkWebview
I think you could try to intercept the itunes link in wkwebview's delegate methods and open the URL using openURL
The below source code will open any itms-appss links in wkwebview. Don't forget to conform to 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
seems not to handle non-http(s) url schemas by default.
So, you have to catch the request using webView(_:decidePolicyFor:decisionHandler:)
, and check the url that can be loaded by WKWebView
.
If the url is non-http(s) url, then you should open the url by open(_:options:completionHandler:)
.
Here the sample code.
Assign the navigationDelegate for your WKWebView
instance.
webView.navigationDelegate = self
Implement WKNavigationDelegate
method.
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)
}
}