111
votes

I already tried getting the current URL of my UIWebView with: webview.request.URL. Unfortunately the NSURL was empty. Anything wrong here? I am working with Xcode 3.2.2 beta 5.

The code above should be executed in the UIWebView delegate didStartLoad.

14
Do that code currentURL = currentWebView.request.URL.absoluteString; after your web view loads so use - (void)webViewDidFinishLoad:(UIWebView *)webView. Maybe this may help.Xcoder
OP, considering this question has 78k views it's a pretty well referenced one. Would you mind updating the accepted answer to the heavily more up-voted one to prevent future confusion? Thanks!Albert Renshaw

14 Answers

317
votes

window.location via JS didn't work reliably for me, but this did:

currentURL = currentWebView.request.URL.absoluteString;

Credit: http://mohrt.blogspot.com/2008/10/getting-url-from-uiwebview.html

55
votes

here's the code I use to grab the url every time you navigate to a different link within the webview:

- (void)webViewDidFinishLoad:(UIWebView *)aWebView
{
  self.url = aWebView.request.mainDocumentURL;
}
45
votes

Matt's version is much cleaner. I recommend everyone to use that one instead of this

You could try this:

NSString *currentURL = [webView stringByEvaluatingJavaScriptFromString:@"window.location"];
36
votes

I too found that the approved answer above was not reliable. But with a slight modification, it seems to work every time:

NSString *currentURL = [webView stringByEvaluatingJavaScriptFromString:@"window.location.href"];

Note the addition of ".href" to the Javascript as this is hidden at the end of the line of code.

18
votes

This is not correct, and will return a nil:

NSString *currentURL = [webView stringByEvaluatingJavaScriptFromString:@"window.location"];

However, the code below can get a URL, but the url may not be the current URL:

NSString *url = _webView.request.URL.absoluteString;

The correct one is:

NSString *currentURL = [_webView stringByEvaluatingJavaScriptFromString:@"window.location.href"];
8
votes
- (void)webViewDidFinishLoad:(UIWebView *)webView{
     NSURL *currentURL = [[webView request] URL];
     NSLog(@"%@",[currentURL description]);

}
4
votes

Tried this for google search results on iPhone:

 NSString* currentURL = webView.request.URL.absoluteString;
 NSString* mainDocumentURL = webView.request.mainDocumentURL.absoluteString;

Both return the same string!

3
votes

here the code i use :

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[[[webView request] URL] absoluteString]]];
3
votes

As UIWebView is deprecated, for WKWebview to get the current url is very simple.

webView.url
2
votes

I use the shouldStartLoadWithRequest event (UIWebViewDelegate) to catch URL updates. request.mainDocumentURL.absoluteString will get you the main web page's URL (which is normally what you want), while request.URL.absoluteString will include CSS and JavaScript includes.

2
votes

This always works . .

NSString* url= [webView stringByEvaluatingJavaScriptFromString:@"window.location.href"];
2
votes

implement delegate method,

- (BOOL)webView:(UIWebView *)webview shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    NSString  *URL = request.URL.absoluteString;    NSLog(@"%@",URL);
}

URL is the what you exactly needed.

1
votes

IN Swift try this,

func webViewDidFinishLoad(webView: UIWebView){
    println(WebView.request?.mainDocumentURL)
}
1
votes

To get current URL of the WKWebView and UIWebview

Here is the code.

if (self.wkWebView) {
   NSString  *URL = self.wkWebView.title;
}else if(self.uiWebView) {
   NSString *URL = self.uiWebView.request.title;
}