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.
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.
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 neededwas 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:
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.
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.
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.
- '
loadRequestcan work for simpler cases.' - Use
WKWebViewinstead ofUIWebViewfor modern code when possible.
Related reading
- Load resources from relative path using local html in uiwebview
- Load Tensorflow js model from local file system in javascript
- Load uservoice API on Click
- loading js files and other dependent js files asynchronously
- Load view from an external xib file in storyboard
- Loaded nib but the 'view' outlet was not set
- Locale based sort in Javascript, sort accented letters and other variants in a predefined way
- localStorage.getItem / .setItem is async?
.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.