UIWebView finished loading Event

强颜欢笑 提交于 2019-11-27 20:23:23

Yes, that's possible. Use the UIWebViewDelegate protocol and implement the following method in your delegate:

- (void)webViewDidFinishLoad:(UIWebView *)webView

If you want the URL, you can get the last request using the property request:

webView.request.URL

None of the found solutions worked for me.

Then I found this example which at least works much better than any other solution I found on Google/StackOverflow.

uiwebview-load-completion-tracker

Very simple method:

Step 1: Set delegate UIWebViewDelegate in header file.

Step 2: Add following webViewDidFinishLoad method to get current URL of webview

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    NSLog(@"Current URL = %@",webView.request.URL);

    //-- Add further custom actions if needed 
}

Pascal's answer for the "getting the URL" part is fine.

However!

From UIWebViewDelegate's documentation, from Apple: "webViewDidFinishLoad: Sent after a web view finishes loading a frame."

Frame != Page.

webViewDidFinishLoad is called when the page is "done loading". It can also be called many times before then. Page loads from Amazon.com can generate a dozen calls to webViewDidFinishLoad.

If you control the page source, then you can make a load test for it, and it will work, for that case. If you only care about getting called "after the page is done loading", then webViewDidFinishLoad is adequate.

For arbitrary pages, with arbitrary JavaScript, loading ad banners in perpetuity, or autoscrolling banners, or implementing a video game, the very idea of a page being "done loading" is wrongheaded.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!