简体   繁体   English

如何在 ios 中加载 webview url 之前执行任务?

[英]how do I perform tasks before loading a webview url in ios?

so I can't seem to figure out a way to perform some tasks before loading a url in ios webview.所以在 ios webview 中加载 url 之前,我似乎无法找到执行某些任务的方法。 Like in android we can use shouldOverrideUrlLoading.就像在 android 中一样,我们可以使用 shouldOverrideUrlLoading。

@Override
public boolean shouldOverrideUrlLoading(final WebView view, String url) {
   ///sometask
}

but I can't find a function that does something like this in ios.但我找不到在 ios 中执行类似操作的 function。 Say a user clicked on a href tag inside the webview that takes it to different page, how do I know what link that tag takes the user to?假设用户点击了 webview 内的 href 标签,将其带到不同的页面,我怎么知道该标签会将用户带到哪个链接? before changing the page.在更改页面之前。

Basically I want to check what url the webpage is about to load and perform extra steps according to that url.基本上我想检查网页即将加载的 url 并根据 url 执行额外的步骤。

You can use WKNavigationDelegate您可以使用 WKNavigationDelegate

set navigationDelegate in viewDidLoad在 viewDidLoad 中设置 navigationDelegate

self.webView?.navigationDelegate = self self.webView?.navigationDelegate = self

Observe URL in below navigationDelegate Method.在下面的 navigationDelegate 方法中观察 URL。

func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: (WKNavigationActionPolicy) -> Void) {
        
        DispatchQueue.main.async {
            if let urlString = navigationAction.request.url?.absoluteString{
                  
                  print(urlString)
             }
         }

    decisionHandler(.allow)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM