113

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.

TheNeil
  • 3,321
  • 2
  • 27
  • 52
danielreiser
  • 5,292
  • 5
  • 31
  • 43
  • Do that code currentURL = currentWebView.request.URL.absoluteString; after your web view loads so use - (void)webViewDidFinishLoad:(UIWebView *)webView. Maybe this may help. – Xcoder Oct 24 '14 at 13:18
  • 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 Apr 14 '17 at 01:21

14 Answers14

317

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

Matt Andersen
  • 4,816
  • 3
  • 24
  • 19
55

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;
}
Mark Sands
  • 1,509
  • 1
  • 14
  • 15
45

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"];
Community
  • 1
  • 1
Rengers
  • 14,911
  • 1
  • 36
  • 54
  • Thanks. My code above works when executing in didFinishLoad.... Somewhere I've read that the js version doesn't work on every page. But I'll try. – danielreiser Mar 23 '10 at 11:12
  • 11
    Matt his version is much cleaner. So yeah I recommend everyone to use that one instead of this. – Rengers Apr 24 '12 at 09:24
  • 1
    This is the only way it works for example in m.youtube.com. All other answers return wrong URLs. – cprcrack Oct 23 '13 at 12:14
  • 2
    I actually evaluate "document.URL" and not "window.location" (which was always blank for me). This is useful for internal adjustments to the page URL on JS heavy sites that might not use traditional page loads. – SmartyP Sep 08 '14 at 19:33
  • 1
    To the two people above - Aubada Taljo and Taranfx - Rengers has noted quite boldly (literally, the text is bold) and humbly that people should use Matt's version with more than 230 likes on it below. Clearly at the time of answering this, it worked and was accepted and 30+ people felt it worked too. This is now 5.5 years old. Good stuff, Rengers. Thanks for pointing people to Matt's answer below. – App Dev Guy Nov 30 '15 at 06:38
36

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.

SarahR
  • 905
  • 8
  • 12
18

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"];
D.D.
  • 333
  • 5
  • 10
8
- (void)webViewDidFinishLoad:(UIWebView *)webView{
     NSURL *currentURL = [[webView request] URL];
     NSLog(@"%@",[currentURL description]);

}
kleopatra
  • 51,061
  • 28
  • 99
  • 211
sivasankar
  • 81
  • 1
  • 1
4

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!

Alex Stone
  • 46,408
  • 55
  • 231
  • 407
4

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

webView.url
LF00
  • 27,015
  • 29
  • 156
  • 295
3

here the code i use :

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[[[webView request] URL] absoluteString]]];
Andy Obusek
  • 12,614
  • 4
  • 41
  • 62
Bobj-C
  • 5,276
  • 9
  • 47
  • 83
  • 5
    You are creating an NSURL by getting the string from an existing NSURL; couldn't you just copy `webView.request.URL`? – Pascal Dec 05 '11 at 16:34
2

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.

Tom Söderlund
  • 4,743
  • 4
  • 45
  • 67
2

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.

Ashish Kakkad
  • 23,586
  • 12
  • 103
  • 136
Gobi M
  • 3,243
  • 5
  • 32
  • 47
2

This always works . .

NSString* url= [webView stringByEvaluatingJavaScriptFromString:@"window.location.href"];
Shaik Riyaz
  • 11,204
  • 7
  • 53
  • 70
1

IN Swift try this,

func webViewDidFinishLoad(webView: UIWebView){
    println(WebView.request?.mainDocumentURL)
}
Mohammad Zaid Pathan
  • 16,304
  • 7
  • 99
  • 130
1

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;
}
Stalin Pusparaj
  • 741
  • 9
  • 11