39

Is it possible to start an event when an UIWebView (Iphone) has finished loading the URL.

How can I find out, the current URL of the UIWebView?

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Ploetzeneder
  • 1,281
  • 4
  • 20
  • 34

4 Answers4

76

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
Pascal
  • 16,846
  • 4
  • 60
  • 69
  • Sorry to wake a dead topic but does the (UIWebview *)webView, is the webView the name of the outlet of your webview name? – Cameron Tarbell Feb 09 '14 at 19:12
  • 1
    @CameronTarbell This is a delegate method, it will pass in the web view that finished loading with the `webView` variable, independent of your outlet name. – Pascal Feb 10 '14 at 20:18
  • ok so the 'webView' variable can be changed to my web view name then. Correct? – Cameron Tarbell Feb 12 '14 at 00:31
  • 1
    @CameronTarbell Depends, it just needs to be a UIWebView, which one I don't care and inside the `webViewDidFinishLoad:` you best just use the variable provided in the method (which is `webView` in my example). – Pascal Feb 12 '14 at 22:58
  • what if websites have iframes? See this for more information: http://stackoverflow.com/questions/2837377/how-to-determine-iframe-finished-loading-in-uiwebview – Peacemoon Apr 20 '15 at 20:51
2

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

sabiland
  • 2,526
  • 1
  • 25
  • 24
1

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 
}
Rajesh Loganathan
  • 11,129
  • 4
  • 78
  • 90
1

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.

Neal
  • 201
  • 2
  • 6