Get current URL of UIWebView

IosIphoneUrlRequestUiwebview

Ios Problem Overview


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.

Ios Solutions


Solution 1 - Ios

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

Solution 2 - Ios

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;
}

Solution 3 - Ios

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"];

Solution 4 - Ios

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.

Solution 5 - Ios

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"];

Solution 6 - Ios

- (void)webViewDidFinishLoad:(UIWebView *)webView{
     NSURL *currentURL = [[webView request] URL];
     NSLog(@"%@",[currentURL description]);

}

Solution 7 - Ios

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!

Solution 8 - Ios

here the code i use :

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

Solution 9 - Ios

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

webView.url

Solution 10 - Ios

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.

Solution 11 - Ios

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.

Solution 12 - Ios

This always works . .

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

Solution 13 - Ios

IN Swift try this,

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

Solution 14 - Ios

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;
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiondanielreiserView Question on Stackoverflow
Solution 1 - IosMatt AndersenView Answer on Stackoverflow
Solution 2 - IosMark SandsView Answer on Stackoverflow
Solution 3 - IosRengersView Answer on Stackoverflow
Solution 4 - IosSarahRView Answer on Stackoverflow
Solution 5 - IosD.D.View Answer on Stackoverflow
Solution 6 - IossivasankarView Answer on Stackoverflow
Solution 7 - IosAlex StoneView Answer on Stackoverflow
Solution 8 - IosBobj-CView Answer on Stackoverflow
Solution 9 - IosLF00View Answer on Stackoverflow
Solution 10 - IosTom SöderlundView Answer on Stackoverflow
Solution 11 - IosGobi MView Answer on Stackoverflow
Solution 12 - IosShaik RiyazView Answer on Stackoverflow
Solution 13 - IosMohammad Zaid PathanView Answer on Stackoverflow
Solution 14 - IosStalin PusparajView Answer on Stackoverflow