Get current URL of UIWebView
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
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:
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:
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
httptohttps - 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:
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:
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.URLtoo 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
UIWebViewinstead of moving toWKWebView.
Summary
- In
UIWebView, the current page URL is commonly available throughwebView.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
- Get day of week using NSDate
- Get device location only country in iOS
- Get file size in Swift
- Get filename and path from URI from mediastore
- Get index in ForEach in SwiftUI
- Get integer value from string in swift
- Get lighter and darker color variations for a given UIColor
- Get notified when UITableView has finished asking for data?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.