[英]Prevent universal links from opening in `WKWebView`/`UIWebView`
When user tap on a universal link in WKWebView
, the corresponding app will be opened (if installed). 当用户点击
WKWebView
的通用链接时,将打开相应的应用程序(如果已安装)。
This is described in Apple Search Programming Guide 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.
如果您实例化SFSafariViewController,WKWebView或UIWebView对象以处理通用链接,iOS将在Safari中打开您的网站,而不是打开您的应用程序。 However, if the user taps a universal link from within an embedded SFSafariViewController, WKWebView, or UIWebView object, iOS opens your app.
但是,如果用户从嵌入式SFSafariViewController,WKWebView或UIWebView对象中点击通用链接,iOS将打开您的应用程序。
In my app, I have a WKWebView
, but I don't want the user to go out of my app. 在我的应用程序中,我有一个
WKWebView
,但我不希望用户退出我的应用程序。 I want to handle the link within my WKWebView
. 我想处理我的
WKWebView
的链接。
How do I prevent universal link from opening? 如何防止通用链接打开? Or can I know if a URL could be handle by other apps?
或者我可以知道其他应用程序是否可以处理URL?
sourcecode for WebKit: 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
如果您使用的是
WKWebView
,只需使用WKNavigationActionPolicyAllow
+ 2而不是WKNavigationActionPolicyAllow
+[LSAppLink openWithURL:completionHandler:] this is how universal link open corresponding app. + [LSAppLink openWithURL:completionHandler:]这是通用链接如何打开相应的应用程序。 you can exchange its implementations with yourself method but be carefule,this is private API.
你可以用自己的方法交换它的实现,但是要小心,这是私有API。
Base on @none 's answer, here is an example for Swift 4 基于@none的回答,这里是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)!)
}
Can you try this? 你能试试吗?
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
decisionHandler(WKNavigationActionPolicy(rawValue: WKNavigationActionPolicy.allow.rawValue + 2)!)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.