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.