ios 如何检查 WkWebView 是否在 Objective-C 中完成加载?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36313253/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 08:47:46  来源:igfitidea点击:

How to check if WkWebView finish loading in Objective-C?

iosobjective-cwkwebviewwkwebviewconfiguration

提问by faklyasgy

I want to load HTML pages using WkWebView and I want to show the page just after it's finished loading. As long as it's loading I would like to show an activity indicator on an empty View. I create two view a loadingView and a wkWebView. While the page is loading I add to VC as subview the loadingView and after I want to remove loadingView and add wkWebView. Here is my code:

我想使用 WkWebView 加载 HTML 页面,并且我想在完成加载后立即显示该页面。只要它正在加载,我就想在空视图上显示活动指示器。我创建了两个视图一个 loadingView 和一个 wkWebView。在页面加载时,我将加载视图的子视图添加到 VC,然后我想删除加载视图并添加 wkWebView。这是我的代码:

    [self addSubview:_loadingView];
    _wkWebView = [[WKWebView alloc] initWithFrame:self.frame];
    _wkWebView.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, self.frame.size.height);

    //Send a request to wkUrlconnection
    NSURL *wkUrl = [NSURL URLWithString:self.wkUrlString];
    NSURLRequest *wkRequest = [NSURLRequest requestWithURL:wkUrl];

    //Here I want to check if it's loaded and then remove loadingView and add wkWebView
    [_wkWebView loadRequest:wkRequest];
    [self.loadingView removeFromSuperview];
    [self addSubview:_wkWebView];

Can someone show me how to check while it's loading and if finish refresh the VC? Thank you for your answers.

有人可以告诉我如何在加载时检查以及是否完成刷新 VC?谢谢您的回答。

回答by Stavash

I think the WKNavigationDelegate's webView:didFinishNavigation:delegate callback is what you're looking for.

我认为WKNavigationDelegate's webView:didFinishNavigation:委托回调就是你要找的。

Configure and present your activity indicator when you start to load and then stop and remove it from view when the callback is called.

当您开始加载时配置并显示您的活动指示器,然后在调用回调时停止并将其从视图中删除。

回答by Nick Kirsten

For anyone who is experiencing the issue of a webpage containing multiple frames and therefore doing multiple loads which interrups your load animation, I have implemented the following and it works for me in all the situations I have come across so far:

对于遇到包含多个框架的网页并因此执行多个加载而中断加载动画的问题的任何人,我已经实现了以下内容并且它适用于我迄今为止遇到的所有情况:

Swift:

迅速:

var loadCount: Int = 0

override func viewDidLoad() {
    super.viewDidLoad()

    startLoading()
    webview.navigationDelegate = self
    let request = URLRequest(url: url)
    webview.load(request)
}

func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
    loadCount += 1
}

func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {

    loadCount -= 1

    DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) { [weak self] in
        if self?.loadCount == 0 {
            self?.stopLoading()
        }
    }

}

The basic idea is to start your load animation before you request the url, then count each request being made and only stop the load animation when your request count == 0. This is done after a slight delay as I find that some frames queue up requests synchronously so the next load will begin before the 0.1 second delay has completed.

基本思想是在请求 url 之前启动加载动画,然后计算每个正在发出的请求,并仅在请求计数 == 0 时停止加载动画。这是在稍微延迟后完成的,因为我发现有些帧在排队同步请求,因此下一次加载将在 0.1 秒延迟完成之前开始。

( ?° ?? ?°)

( ?° ?? ?°)

回答by ingconti

for swift 4.2:

对于 swift 4.2:

func webView(_ webView: WKWebView,
                 didFinish navigation: WKNavigation!){
        print("loaded")
}

be sure to set delegate for webView in didLoad (or similar)

一定要在 didLoad(或类似的)中为 webView 设置委托

webView.navigationDelegate = self