UIWebView
URL Retrieval
iOS Development
Mobile App Development
Objective-C

Get current URL of UIWebView

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

In legacy iOS code that still uses UIWebView, the current page URL is usually available through the web view's request. The simplest answer is often webView.request.URL, but the right place to read it depends on whether the page is still loading, has redirected, or was navigated by script.

That timing detail is why developers sometimes think the value is missing or stale. The API is simple, but reading it at the right lifecycle moment matters.

Read the URL From the Request

For a basic UIWebView, the currently loaded request can be inspected directly:

objective-c
1#import <UIKit/UIKit.h>
2
3@interface ViewController : UIViewController <UIWebViewDelegate>
4@property (nonatomic, strong) UIWebView *webView;
5@end
6
7@implementation ViewController
8
9- (void)viewDidLoad {
10    [super viewDidLoad];
11
12    self.webView = [[UIWebView alloc] initWithFrame:self.view.bounds];
13    self.webView.delegate = self;
14    [self.view addSubview:self.webView];
15
16    NSURL *url = [NSURL URLWithString:@"https://example.com"];
17    NSURLRequest *request = [NSURLRequest requestWithURL:url];
18    [self.webView loadRequest:request];
19}
20
21- (void)webViewDidFinishLoad:(UIWebView *)webView {
22    NSLog(@"Current URL: %@", webView.request.URL.absoluteString);
23}
24
25@end

webView.request.URL is the most common property to inspect. Inside webViewDidFinishLoad:, it usually reflects the final URL after the current navigation completed.

Use Delegate Methods for Navigation Changes

If you want the URL while navigation is happening, the delegate methods are the safest place to track it.

For example:

objective-c
1- (BOOL)webView:(UIWebView *)webView
2shouldStartLoadWithRequest:(NSURLRequest *)request
3 navigationType:(UIWebViewNavigationType)navigationType {
4    NSLog(@"About to load: %@", request.URL.absoluteString);
5    return YES;
6}
7
8- (void)webViewDidFinishLoad:(UIWebView *)webView {
9    NSLog(@"Finished loading: %@", webView.request.URL.absoluteString);
10}

This gives you two useful views of navigation:

  • 'shouldStartLoadWithRequest: shows the request about to start'
  • 'webViewDidFinishLoad: shows the page after the load completed'

If redirects occur, you may see several requests before the final destination settles.

Understand Redirects and JavaScript Navigation

The "current URL" can be more complicated than it first appears. A page might:

  • redirect from http to https
  • perform server-side redirection
  • change location through JavaScript
  • load an iframe while keeping the main page URL the same

That is why checking the request once in viewDidLoad is often not enough. If you need the final page address that the user actually sees, webViewDidFinishLoad: is usually the better place to inspect it.

For additional verification in tricky cases, developers sometimes read the browser location from JavaScript:

objective-c
NSString *location = [self.webView stringByEvaluatingJavaScriptFromString:@"window.location.href"];
NSLog(@"JS location: %@", location);

That can help when the request object and the page state do not seem to match your expectation. It is still best treated as a fallback, not the first choice.

UIWebView Is Deprecated

It is worth stating plainly: UIWebView is deprecated and should not be used in new iOS code. WKWebView replaced it with better performance, stronger security, and more predictable navigation APIs.

The WKWebView equivalent is clearer:

objective-c
NSLog(@"%@", webView.URL.absoluteString);

So if you are working on a codebase that still uses UIWebView, retrieving the URL is possible, but migration should remain on the roadmap.

Common Pitfalls

  • Reading webView.request.URL too early, before the navigation finished.
  • Assuming the first requested URL is the final page URL after redirects.
  • Ignoring JavaScript-driven navigation when debugging unexpected values.
  • Treating subresource loads or iframe behavior as if they were the top-level document URL.
  • Continuing to build new functionality on UIWebView instead of moving to WKWebView.

Summary

  • In UIWebView, the current page URL is commonly available through webView.request.URL.
  • 'webViewDidFinishLoad: is usually the best place to read the final URL of a completed navigation.'
  • 'shouldStartLoadWithRequest: is useful for tracking requests as they begin.'
  • Redirects and JavaScript navigation can make timing important.
  • For modern iOS work, prefer WKWebView, which exposes the page URL more cleanly.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.