0

Here is a little problem I just noticed in one of my iOS apps.

I use Xcode Version 10.1 and Swift 4.2.

The app has a button which when pushed brings up a view controller, this VC is in charge of opening a link to the app itself in itunes. I have done this many times with no problems in the past.

But this time, a blank pages opens up and nothing else. I have tried to replace only the URL I am interested in by "https://www.google.com/" and it works perfectly as expected (i.e. the Google page shows up). Of course I have verified that my URL is correct.

Can anybody see what could be the issue? Thanks in advance for any relevant tip.

Here is the code for the whole view controller.

import UIKit
import WebKit

class appStore_ViewController: UIViewController, WKNavigationDelegate {
    let appDelegate:AppDelegate = UIApplication.shared.delegate as! AppDelegate
    var webView: WKWebView!

    override func loadView() {
        webView = WKWebView()
        webView.navigationDelegate = self
        view = webView
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        let appStoreURL = URL(string: "https://itunes.apple.com/app/id\ (appDelegate.applicationID)?mt=8")
        //let appStoreURL = URL(string: "https://www.google.com/") // This works as     expected.
        print("The link: \(appStoreURL!.absoluteString)") // This shows what is expected     (a working URL).
        webView.load(URLRequest(url: appStoreURL!))
    }
}
Michel
  • 10,303
  • 17
  • 82
  • 179

2 Answers2

1

You should see what happen while visiting the AppStore Url with safari browser in your iPhone. then you will find the it still show blank page but show an alert to open the Appstore.

If you want to do the same function in your app with webView. you have to handle the alert the by yourself.

lee Tom
  • 11
  • 1
  • I see. Indeed when I past the link in Safari on my iPhone it shows an alert: "Do you want to open the app store", instead of a blank page like in my app. How can I handle this inside my app? (The URL has no problem to open on my mac.) – Michel Jan 28 '19 at 09:32
0

You need to implement the decidePolicyForNavigationAction method of WKNavigationDelegate for this to work.

    - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)

    (WKNavigationActionPolicy))decisionHandler {
    /* Code to handle other schemes such as file, https, http, etc. if required
    */
    if ([[[navigationAction.request URL] scheme] isEqual:@"itms-appss"])
  {

        [[UIApplication sharedApplication] openURL:[navigationAction.request URL]];
        decisionHandler(WKNavigationActionPolicyCancel);
        return;
   }
}

WKWebview does not inherently support different schemes such as "itms-appss" and you need to check and implement the url scheme you want to support.

You also need to check if you are running in iPadOS and set WKWebpagePreference to use mobile content mode if you do so.