Link in iOS app not working any more

Weird. If I push the link for the second time, it opens the app store preview page. (on the mac)

So I opened the Hype project in Hype Reflect.

Go the same issue. Then used the button to open in safari.

When I click the button there I get the alert to open the App Store.

So this is looking more and more that our apps cannot handle the itms redirect protocol/scheme.

It may be a simple thing to change that but not sure…

The does seem to be a scheme management for wkwebview

https://developer.apple.com/documentation/webkit/wkwebviewconfiguration/2875766-seturlschemehandler

But you are on your own there… :sunglasses:

Right. I will go on looking for a solution. It definitely has something to do with the wkwebview. (old app did not have the problem). Maybe someone else who is reading this thread has an answer? For now I’m on my own :disappointed_relieved:
Thank you very much for your time and efforts.

Hah,

I took a punt and got it working.

This code just checks what is about to load and then has the iOS Application itself open the link.
I figured this would bypass the web view trying…

I added the below to my iOS app code. The delegate/ same file with all the wkwebview code

  public func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Swift.Void)
    {
        
            if navigationAction.request.url != nil
            {
                
                print("\(navigationAction.request.url!)")
                //do what you need with url
               
                guard let url = URL(string: "\(navigationAction.request.url!)") else {
                    return //be safe
                }
                
                if #available(iOS 10.0, *) {
                    UIApplication.shared.open(url, options: [:], completionHandler: nil)
                } else {
                    UIApplication.shared.openURL(url)
                }
                
            }
            decisionHandler(.allow)
        
        
    }

A couple of things this code will need sanitising.
Read up in the documentation about this scheme management

Also there probably is much simpler way of doing it. Like the button posts a message to the App and that runs the open code. In fact that sounds a much better idea since there is no real learning curve about scheme prefs and you are not interfering with it.


UPDATE,

Yep the post message works and gives better control.

Simple set up which can be changed for you needs.

I gave the button the id of the url. But would suggest you give it a normal id and match against it in the hype function to then post the relevant url. Or do that in the iOS app part and just post the id.

But anyway… in my quick test version.

Button ID
romeinse-cijfers-ivxlcdm/id1116400243?l=nl&ls=1&mt=8

The Button calls a js function in Hype.

window.webkit.messageHandlers.openStore.postMessage(element.id);

In the iOS app viewDidLoad

I set the post message name. openStore

override func viewDidLoad() {
        
        super.viewDidLoad()
        
        //-- configure the WKWebView
        
        //-- init config and controller
        let wconfiguration = WKWebViewConfiguration()
        let wcontroller = WKUserContentController()
        
        //-- We must add the webkit scripts Posted messages name we expect to get from the Hype Page to the controller
      
        wcontroller.add(self, name: "openStore")
.....

And then write the handler delegate function for the post messages like so

//-- webkit Posted messages  handler delegate //-- handles messages from hype page
    func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
        
        
        if message.name == "openStore"{
            
           // anURL  = URL.init(string: message.body as! String)!
            guard let url = URL(string: "https://itunes.apple.com/us/app/\(message.body)") else {
                return //be safe
            }
            
            if #available(iOS 10.0, *) {
                UIApplication.shared.open(url, options: [:], completionHandler: nil)
            } else {
                UIApplication.shared.openURL(url)
            }
          
        }
        
         
    }

Thats it.

1 Like

updated last post

Wow, nice work. But:

Did I do something wrong?

This is what I have above the “override”

15

plenty.

I have posted a few post already on how to write all this but lets take this to private for the mo.