8

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

Shiva
  • 545
  • 1
  • 10
  • 41
dinesh R
  • 379
  • 3
  • 11

3 Answers3

10

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;
    }
Durai Amuthan.H
  • 31,670
  • 10
  • 160
  • 241
  • any ideas on which delegate I could try ... do I have to check for this particular URL " https://itunes.apple.com/gb/app/xx-yy-zz/id435919263?mt=8" and if found ... use the code snippet to open separately – dinesh R Nov 13 '19 at 11:52
  • 1
    You can look for the **scheme** and whether it's **itms-apps** then you can open the link using **openURL** or else you will have to put so many conditional checks for every itunes link that your webview's going to show and you may have to re-release the app everytime some new itunes links are added in the webpageto add a conditional check for that. – Durai Amuthan.H Nov 13 '19 at 11:59
  • 1
    Please look into **WKNavigationDelegate** protocol methods , you will be able to find yourself. – Durai Amuthan.H Nov 13 '19 at 12:03
  • I will look into that in the mean time , if you have any suggestion on which delegate method to look for , let me knwo – dinesh R Nov 13 '19 at 12:10
  • 1
    Try to intercept the URL here **- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler** – Durai Amuthan.H Nov 14 '19 at 11:38
  • now the app opens successfully in appstore but my app crashes.i'm looking into that – dinesh R Nov 14 '19 at 23:15
  • 1
    @dineshR Make sure to call decisionHandler closure inside your delegate method implementation – dosi Nov 15 '19 at 00:07
  • Did not work with my http links to App Store (links were opened in the WKWebView and not redirected to the App Store app). So I use the same trick as described in this answer but checked if the URL contains the string "itunes.apple.com", and in this case I use [[UIApplication sharedApplication] openURL:url]; i – Pierre Dec 09 '20 at 09:13
4

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 WKWebViewinstance.

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)
    }
}
Kazunori Takaishi
  • 2,268
  • 1
  • 15
  • 27
0

Try in your info.plist adding:

App transport Security settings -- Allow Arbitrary loads = yes

<key>NSAppTransportSecurity</key>
<dict>
    <!--Connect to anything (this is probably BAD)-->
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>
Karl
  • 1
  • Can you expand your answer a bit about what does this bit of code do and why is it necessary? – Tom Nov 08 '19 at 02:00
  • Apple implemented a "feature" to protect the user. you are not allowed to link to non https addresses, or it stops you with that error. the plist entry overrides it. I have a couple apps that use my own API, so i had to also over-ride it. You can over-ride it and it will still go through review. – Karl Nov 08 '19 at 03:18
  • that override allows all links. so even if your https redirects to a non secure site (http), it should let it go.. im using http, but it can be any non-secure protocol. – Karl Nov 08 '19 at 03:23
  • @Karl - Why is it not happening with UIWebview ? – dinesh R Nov 08 '19 at 10:00