Ready to Level-Up?

This just in! If your rusty iOS skills could use some refining, or if you're just starting out and you want to level-up fast, consider signing up for our upcoming iOS Screencasts

New from iOS Rocket Surgery Swift Video Tutorials: The rapidly evolving Apple-iOS ecosystem has just been thrown into tumult with the recent introduction of the Swift Programming Language. Are you ready to finally try iOS development? Are you needing to switch from Objective-C to Swift? Sign up today for our free video courses!
(By the dear folks at Swift Tutorial Videos, a sister company)

Saturday, December 14, 2013

How To Get The Current UIWebView URL

So, you've got a webview and you want to know what the URL it has loaded is, eh?

A webview is an iOS UI component that allows us to render HTML, whether fetched and loaded from a remote URL, pulled from the file-system, or manually (and arduously) created inline as a NSString or some such in your code. But, what if you load a remote URL and allow the user to click on links within the document, and they are subsequently directed to various pages on the web... if we wanted to check up on the user[1] and find out where they've gone, how could we do that?

So how do we tell what URL is currently loaded in our webview? We use what feels a bit like a trick, which is to ask the webview to evaluate some Javascript for us that will return the DOM window's current location.

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

Easy-peasy, and if you think about it, evaluating Javascript like this within the page opens up a lot of introspection opportunities. Now go forth and use this power for good.

[1] An aside, and not very important to this discussion: In my case I care about where the user goes after they are redirected from the authentication endpoint, since I use omni-auth on my server-side and redirect my users to authorize my app from there (rather than the typical case of saving authentication information local in my iOS app). So when they reach a success message, or a particular URL, I want to close the authorization screen. Works nice.

About Me

Easy NSDateFormatter Tool

Save yourself some time in formatting your NSDates to NSStrings, and use the Blind NSDate app, which you can download from iTunes. There's also a website where you can format your NSDates:

Popular Posts

Designed By Seo Blogger Templates