HTML
UIWebView
iOS Development
Web Page Title
Objective-C

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.

Browse interview questions

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.

objective-c
1#import "ViewController.h"
2
3@interface ViewController () <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    NSString *title =
23        [webView stringByEvaluatingJavaScriptFromString:@"document.title"];
24    NSLog(@"Page title: %@", title);
25    self.title = title;
26}
27
28@end

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:

objective-c
1NSString *html =
2    @"<html><head><title>Local Example</title></head>"
3     "<body><h1>Hello</h1></body></html>";
4
5[self.webView loadHTMLString:html baseURL:nil];

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.

objective-c
1[self.webView evaluateJavaScript:@"document.title"
2               completionHandler:^(id result, NSError *error) {
3    if (error == nil) {
4        NSLog(@"Page title: %@", result);
5    }
6}];

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.title before webViewDidFinishLoad: 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.
  • 'UIWebView is 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 with document.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 WKWebView instead of UIWebView.

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.