Swift
UIWebView
Local HTML
iOS Development
Mobile App Development

Load local html into UIWebView using swift

Interview Questions practice on Codemia

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

Browse interview questions

Introduction

UIWebView is deprecated, but older iOS projects still contain it, and one common maintenance task is loading bundled HTML into the view correctly. The key detail is not just loading the HTML string itself, but also giving the web view a proper base URL so relative links to CSS, JavaScript, and images resolve from the app bundle.

Load an HTML File from the App Bundle

If the HTML file is packaged with the app, first locate it in the bundle.

swift
1import UIKit
2
3class ViewController: UIViewController {
4    @IBOutlet weak var webView: UIWebView!
5
6    override func viewDidLoad() {
7        super.viewDidLoad()
8
9        if let url = Bundle.main.url(forResource: "index", withExtension: "html") {
10            do {
11                let html = try String(contentsOf: url, encoding: .utf8)
12                webView.loadHTMLString(html, baseURL: url.deletingLastPathComponent())
13            } catch {
14                print(error)
15            }
16        }
17    }
18}

The baseURL is important. Without it, relative asset paths in the HTML may fail.

Make Sure the File Is in the Target Bundle

This often fails for a simple reason: the HTML file was added to the Xcode project but not included in the app target.

Check that:

  • the HTML file is inside the project
  • 'Copy items if needed was selected when it was added'
  • the file appears in the target membership settings

If the file is missing from the bundle, Bundle.main.url(forResource:withExtension:) returns nil and the web view never loads anything.

Relative Assets Need the Base URL

Suppose index.html contains:

html
<link rel="stylesheet" href="styles.css">
<img src="images/logo.png">

Those files will only resolve correctly if the base URL points to the folder containing the HTML file. That is why url.deletingLastPathComponent() is usually the right base URL instead of nil.

You Can Also Load the File Directly

For very simple local pages, another option is to create a request from the file URL.

swift
1if let url = Bundle.main.url(forResource: "index", withExtension: "html") {
2    let request = URLRequest(url: url)
3    webView.loadRequest(request)
4}

This can work well for a self-contained HTML file, though explicit loadHTMLString(..., baseURL: ...) is often clearer when you know the page depends on nearby assets.

If the HTML depends on bundled JavaScript or styles, test those references directly after loading. A blank page is often not a rendering bug at all. It is usually one missing local asset or one incorrect relative path in the HTML.

Prefer WKWebView in Modern Code

If you are not maintaining a legacy codebase, use WKWebView instead. The idea is similar, but the API and performance characteristics are better and the class is supported.

swift
1import WebKit
2
3if let url = Bundle.main.url(forResource: "index", withExtension: "html") {
4    let html = try String(contentsOf: url, encoding: .utf8)
5    let webView = WKWebView(frame: .zero)
6    webView.loadHTMLString(html, baseURL: url.deletingLastPathComponent())
7}

This is worth mentioning because many developers encounter old UIWebView examples while working on apps that should no longer use it.

Common Pitfalls

A common mistake is loading the HTML string with baseURL: nil and then wondering why images or styles do not appear.

Another is forgetting target membership, which makes the file unavailable at runtime even though it is visible in Xcode.

Developers also sometimes debug the web view itself when the real issue is simply that the asset paths in the HTML are wrong relative to the bundle structure.

Summary

  • Load bundled HTML by locating it with Bundle.main.url(forResource:withExtension:).
  • Use loadHTMLString(..., baseURL: ...) when the page has relative assets.
  • Confirm the HTML file is actually included in the app target.
  • 'loadRequest can work for simpler cases.'
  • Use WKWebView instead of UIWebView for modern code when possible.

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.