How to Get the Title of a HTML Page Displayed in UIWebView?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In a legacy iOS project, the usual way to read the title of a page shown in UIWebView is to evaluate document.title after the page finishes loading. The key detail is timing: if you ask too early, the title will be empty because the HTML has not finished loading yet.
Read the Title After the Load Completes
UIWebView loads content asynchronously, so you should wait for the delegate callback webViewDidFinishLoad: before reading JavaScript values from the page.
The important line is document.title. That JavaScript expression returns the contents of the page's title element. stringByEvaluatingJavaScriptFromString: runs that expression inside the loaded page and gives the result back as an NSString.
Why the Delegate Method Matters
If you call stringByEvaluatingJavaScriptFromString: immediately after loadRequest:, you are racing the network and the rendering engine. In some cases it may appear to work during local testing, then fail on slower networks or on pages that modify the title later with JavaScript.
The delegate callback is the minimum safe point for traditional pages. It tells you the web view finished one load pass, which is usually enough to read the initial title.
Handling Local HTML Content
The same technique works if you load a string instead of a remote URL:
Once webViewDidFinishLoad: fires, document.title returns Local Example in exactly the same way.
Prefer WKWebView in Modern Code
UIWebView is deprecated, so new code should use WKWebView. The modern version uses asynchronous JavaScript evaluation with a completion handler.
This is safer because you get an explicit error object and you are using the supported web view API that current iOS projects should rely on.
When the Title Changes After Load
Some pages set the title dynamically after the initial document load, especially sites driven by JavaScript frameworks. In those cases, webViewDidFinishLoad: may give you the original title but not the final one shown to the user later. For legacy code, the usual workaround is to evaluate document.title again after the page triggers a known action or after a short delay, although that is much less clean than modern web-view APIs.
For simple static pages, though, the delegate method is still the right and sufficient place to read the title.
Common Pitfalls
- Calling
document.titlebeforewebViewDidFinishLoad:often returns an empty string. - Single-page apps may update the title after the initial load, so you may need additional coordination for highly dynamic pages.
- '
UIWebViewis deprecated and should only appear in legacy maintenance work.' - If JavaScript is disabled or blocked in a special environment, title extraction through script evaluation will fail.
Summary
- In
UIWebView, get the page title withdocument.title. - Run that JavaScript in
webViewDidFinishLoad:so the page has finished loading first. - The returned string can be logged, displayed, or assigned to the navigation title.
- For modern iOS development, use
WKWebViewinstead ofUIWebView.
Related reading
- How to handle errors from setTimeout in JavaScript?
- How to handle promise when calling async JS method from C using Emscripten
- How to handle UnprocessedItems using AWS JavaScript SDK dynamoDB?
- How to have a processor intensive function update every tick of the metronome and draw to canvas using web workers?
- How to get the width and height of an android.widget.ImageView?
- How to get TimeZone from android mobile?
- How to implement a Promise retry and undo
- How to initialize an array's length in JavaScript?
.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.