Search code examples
ioswkwebviewitms-90809

Itunes app links are not working with WkWebview


One of the webpage I load into Wkwebview has the following iTunes app link

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

when it's opened I'm getting the following alert

enter image description here

and here's the error that I've got.

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

When the same iTunes link ( https://itunes.apple.com/gb/app/xx-yy-zz/id435919263?mt=8 ) is opened in UIWebview , I saw that URL gets redirected to following URL and app opens in appstore

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

Whereas in Wkwebview , the URL gets redirected to following URL

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

Any help is appreciated


Update

I even tried Arbitrary uploads to true for transport security and the problem is still there.

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)}


Solution

  • 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;
        }