I have an iOS universal link targeted at myApp
. When I click that link in another app, myApp
opens and displays the right payoff perfectly, it's working.
But myApp
includes a built-in browser using WKWebView
. When I click the same universal link from within my built-in browser, iOS doesn't send the link to myApp
, it goes and fetches a webpage.
Apple docs say
If you instantiate a SFSafariViewController, WKWebView, or UIWebView object to handle a universal link, iOS opens your website in Safari instead of opening your app. However, if the user taps a universal link within an embedded SFSafariViewController, WKWebView, or UIWebView object, iOS opens your app.
I note this similar question where the suggestion was to define a WKWebView
delegate. I have both WKWebView
delegates defined and in use in myApp
, and it's not helping. This other question has lots of upvotes but no answers.
My WKWebView
can actually open universal links to other apps. If I click the link https://open.spotify.com/artist/3hv9jJF3adDNsBSIQDqcjp from within the myApp
WKWebView
then it opens the spotify app without opening any intermediate webpage (even though the long-press menu in myApp
doesn't offer to "open in spotify"). But iOS will not deliver the universal link to myApp
when I click it from within myApp
.
After much testing, I discover that I can prevent the display of a webpage associated with the universal link by looking for the specific URL and cancelling the display:
func webView(webView: WKWebView, decidePolicyForNavigationResponse navigationResponse: WKNavigationResponse, decisionHandler: (WKNavigationResponsePolicy) -> Void) {
if let urlString = navigationResponse.response.URL?.absoluteString {
if urlString.hasPrefix(myULPrefix) { // it's a universal link targetted at myApp
decisionHandler(.Cancel)
return
}
}
decisionHandler(.Allow)
}
I have to do this in the decision handler for the response, not the one for the action. When I do this, the universal link is queued for delivery to myApp
. BUT it is not actually delivered by iOS until I quit my app (for example, by hitting the home button) and relaunch it. The appDelegate
function specified as delivering this message in the Apple docs referenced above
func application(application: UIApplication, continueUserActivity userActivity: NSUserActivity, restorationHandler: ([AnyObject]?) -> Void) -> Bool {}
is not called. It is called when I do a more conventional deeplink - clicking a universal link in Safari to open myApp
.
Is this a bug? Or a feature? Or am I, as usual, just barking up the wrong tree?
Thanks!