Prevent universal links from opening in `WKWebView`/`UIWebView`

continueuseractivity universal links
testing universal links
how to use universal link ios
ios universal links
universal link ios tutorial
universal link well known
universal links ssl
universal link redirect to app store

When user tap on a universal link in WKWebView, the corresponding app will be opened (if installed).

This is described in Apple Search Programming Guide

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 from within an embedded SFSafariViewController, WKWebView, or UIWebView object, iOS opens your app.

In my app, I have a WKWebView, but I don't want the user to go out of my app. I want to handle the link within my WKWebView.

How do I prevent universal link from opening? Or can I know if a URL could be handle by other apps?

sourcecode for WebKit:

static const WKNavigationActionPolicy WK_API_AVAILABLE(macosx(10.11), ios(9.0)) _WKNavigationActionPolicyAllowWithoutTryingAppLink = (WKNavigationActionPolicy)(WKNavigationActionPolicyAllow + 2);

if you are using WKWebView, just use WKNavigationActionPolicyAllow + 2 instead of WKNavigationActionPolicyAllow

Complete Guide to Implementing WKWebView, WKWebView does not open any links which have target="_blank" aka. This will be come the similar behavior like UIWebView which always open new window in the Prevent universal links from opening in `WKWebView`/`UIWebView`. 1. I’m not sure I fully understand your question. Just to cover basics, I’ll allow Apple to explain what a Universal Link is: . Universal links let users open your app when they tap links to your website within WKWebView and UIWebView views and Safari pages, in addition to links that result in a call to openURL:, such as those that occur in Mail, Messages, and other apps.

Base on @none 's answer, here is an example for Swift 4

I've tested it and it does work!

func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
    decisionHandler(WKNavigationActionPolicy(rawValue: WKNavigationActionPolicy.allow.rawValue + 2)!)
}

ios8, Prevent universal links from opening in `WKWebView`/`UIWebView`. When user tap on a universal link in WKWebView, the corresponding app will be opened (if  Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. Learn more Disable WKWebView for opening links to redirect to apps installed on my iPhone

+[LSAppLink openWithURL:completionHandler:] this is how universal link open corresponding app. you can exchange its implementations with yourself method but be carefule,this is private API.

you can check LSAppLink head file here

Wkwebview links not working, ios - Prevent universal links from opening in `WKWebView`/`UIWebView` - Stack Overflow. When user tap on a universal link in WKWebView, the corresponding  Universal links let users open your app when they tap links to your website within WKWebView and UIWebView views and Safari pages, in addition to links that result in a call to openURL:, such as those that occur in Mail, Messages, and other apps.

Can you try this?

func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
    decisionHandler(WKNavigationActionPolicy(rawValue: WKNavigationActionPolicy.allow.rawValue + 2)!)
}

WKWebView for Subframes, Global Nav Open Menu Global Nav Close Menu; Apple Developer Use the stopLoading() method to stop loading, and the isLoading property to find When a Phone link is tapped, the Phone app launches and dials the number. the evaluateJavaScript(_:completionHandler:) method of the WKWebView class instead. How do I prevent links from opening in Edge in windows 10 Despite my default browser being other than Edge, all links open in this terrible bit of software. This means that instead of having a friendly browser that knows my passwords and usernames to all sites, I have to remember them and type them in every time.

Prevent the opening of installed apps · Issue #268 · duckduckgo/iOS , Universal links are Apple's way of launching apps on their operating system from a it's the deep linking experience which is preventing them from accessing the WKWebView, or UIWebView object to handle a universal link, iOS opens your​  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 from within an embedded SFSafariViewController, WKWebView, or UIWebView object, iOS opens your app.

Prevent universal links from opening in `WKWebView`/`UIWebView`, Like all all ATS settings, this is configured inside your application's Info.plist file, and this is one of the very few times when editing your plist as source code is faster than trying to use the GUI editor in Xcode. So, right-click on your Info.plist and choose Open As > Source Code. Your plist should end like this: </dict> </plist>

UIWebView, Examples include the Twitter app which brings up links in tweets rather than opening them in Safari. Let's say you need to display a web page in your app. You would create a UIWebView and tell it to load that page. It has been replaced with WKWebView (which is what Safari uses) from iOS 8 onwards but it's not deprecated yet.

Comments
  • hk.saowen.com/a/… This blog is more detail about this. and the way we apply : func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) { return decisionHandler((WKNavigationActionPolicy(rawValue: WKNavigationActionPolicy.allow.rawValue + 2)!)) }
  • does not work for iOS 11 anymore. additionally, it's private API.