Vincent Gable’s Blog

April 22, 2009

Getting the Current URL from a WebView

Filed under: Cocoa,iPhone,MacOSX,Objective-C,Programming,Sample Code | , , ,
― Vincent Gable on April 22, 2009

UPDATED 2009-04-30: WARNING: this method will not always give the correct result. +[NSURL URLWithString:] requires it’s argument to have unicode characters %-escaped UTF8. But stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding will convert # to %23, so http://example.com/index.html#s1 would become http://example.com/index.html%23s1. Unfortunately, the two URLs are not equivalent. The un-%-escaped one refers to section #s1 in the file index.html, and the other tries to fetch the file index.html#s1 (“index dot html#s1”). I have not yet implemented a workaround, although I suspect one is possible, by building the NSURL out of bits of the JavaScript location object, rather then trying to convert the whole string.


UIWebView/WebView does not provide a way to find the URL of the webpage it is showing. But there’s a simple (and neat) way to get it using embedded JavaScript.

- (NSString *)stringByEvaluatingJavaScriptFromString:(NSString *)script

Is a deceptively powerful method that can execute dynamically constructed JavaScript, and lets you embed JavaScript snippets in Cocoa programs. We can use it to embed one line of JavaScript to ask a UIWebView for the URL it’s showing.

@interface UIWebView (CurrentURLInfo)
- (NSURL*) locationURL;
@end
@implementation UIWebView (CurrentURLInfo) - (NSURL*) locationURL; { NSString *rawLocationString = [self stringByEvaluatingJavaScriptFromString:@"location.href;"]; if(!rawLocationString) return nil; //URLWithString: needs percent escapes added or it will fail with, eg. a file:// URL with spaces or any URL with unicode. locationString = [locationString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; return [NSURL URLWithString:locationString] } @end

With the CurrentURLInfo category, you can do aWebView.locationURL to get the URL of the page a WebView is showing.


One last note, the

if(!rawLocationString)
	return nil;

special-case was only necessary, because [NSURL URLWithString:nil] throws an exception (rdar://6810626). But Apple has decided that this is correct behavior.

5 Comments »

  1. Wow!This could be used with UIWebView classes in finding the Url’s of the web pages,its very useful.Nice post.

    Comment by Jim Eric — April 23, 2009 @ 1:53 pm

  2. Very clever! Using this method in an app. Just a heads up, you mixed up the use of “rawLocationString” and “locationString” in the locationURL method. You also forgot a semicolon after the return at the end. Seriously, really clever method of getting the current URL

    Comment by Hunter Bridges — October 23, 2010 @ 3:19 pm

  3. Thanks for these information,also I want to ask something,what is your wordpress blog theme name ?I really like it…

    Comment by Prefabrik — February 17, 2011 @ 2:15 pm

  4. Thank you :-), it’s actually just the old WordPress Classic theme, with little tweaks over the years. I wish I could be more helpful than that, but that’s really all there is to it, and unfortunately it’s not really clean enough to share (I’ve had it break a few times during upgrades).

    Comment by Vincent Gable — February 17, 2011 @ 3:17 pm

  5. clever hack :) stringByEvaluatingJavaScriptFromString is magic

    Comment by Nur Nachman Eytan — November 3, 2011 @ 12:20 pm

RSS feed for comments on this post.

Leave a comment

Powered by WordPress