Reputation: 1321
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?
Upvotes: 39
Views: 39206
Reputation: 201
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.
Upvotes: 1
Reputation: 2614
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
Upvotes: 2
Reputation: 11217
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
}
Upvotes: 1
Reputation: 16941
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
Upvotes: 76