<img src="image.jpg"> resolves against nothing and the file is never found. Pass the bundle directory as the base URL — webView.loadHTMLString(html, baseURL: Bundle.main.bundleURL) — and the same relative path starts working. On WKWebView, use loadHTMLString(_:baseURL:) or loadFileURL(_:allowingReadAccessTo:).Why this happens
When you call loadHTMLString(_:baseURL:) with a nil base URL, the web view has no directory to resolve relative paths against. <img src="image.jpg"> is a relative URL, so the loader tries to resolve it against an empty origin and gives up. The image is in the bundle the whole time — the web view just never looks there.
The second half of the trap is the workaround people try next: building an absolute file:// path with Bundle.main.path(forResource:ofType:) and dropping it into the src attribute. That does not work either. A file:// URL inside an HTML string loaded without a base URL is treated as cross-origin and blocked, so the image still fails to appear — often with no error at all, just a blank box.
The fix is not to change the HTML. It is to tell the web view which directory the HTML belongs to, so relative paths resolve to real files on disk.
How to fix it
1. Pass the main bundle as the base URL (UIWebView)
The bundle directory is the folder that contains your compiled resources. Use it as the base URL and keep your src attributes relative:
import UIKit
let html = """
<html>
<body>
<a href="https://example.com"><img src="myimage.png" width="200"></a>
</body>
</html>
"""
webView.loadHTMLString(html, baseURL: Bundle.main.bundleURL)
Bundle.main.bundleURL is the .app directory itself, which is exactly where a resource added to Copy Bundle Resources ends up on iOS. The Objective-C equivalent is the same idea:
NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];
[webView loadHTMLString:htmlString baseURL:baseURL];
If your image lives in a subfolder that you added as a folder reference (blue folder in the project navigator), the folder name becomes part of the path: <img src="images/myimage.png">. If you added it as a plain group, the files are flattened into the bundle root and the folder name must be dropped.
2. Move to WKWebView and use the same base URL
UIWebView has been deprecated for years and App Review rejects new apps that still link against it, so the durable fix is to port the screen to WKWebView. The API is nearly identical:
import UIKit
import WebKit
final class HelpViewController: UIViewController {
private let webView = WKWebView()
override func viewDidLoad() {
super.viewDidLoad()
webView.frame = view.bounds
webView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
view.addSubview(webView)
let html = "<html><body><img src=\"myimage.png\"></body></html>"
webView.loadHTMLString(html, baseURL: Bundle.main.bundleURL)
}
}
WKWebView resolves relative paths against the base URL the same way, so nothing else in your markup changes.
3. If you ship an HTML file instead of a string, use loadFileURL
A common mistake with WKWebView is calling load(URLRequest(url: fileURL)) for a bundled HTML file. That silently fails — WKWebView refuses file:// requests through the normal navigation path. Use the dedicated method and grant read access to the containing directory so sibling images and CSS load too:
guard let fileURL = Bundle.main.url(forResource: "help", withExtension: "html") else { return }
webView.loadFileURL(
fileURL,
allowingReadAccessTo: fileURL.deletingLastPathComponent()
)
With allowingReadAccessTo set to the folder, <img src="myimage.png"> inside help.html resolves to the file sitting next to it in the bundle. Narrow the directory if you keep assets in a subfolder — granting the whole bundle works but is broader than necessary.
4. Confirm the file is actually in the built product
Before changing any code, verify the resource survived the build:
- Select the target, open Build Phases → Copy Bundle Resources, and check the image is listed. A file that is only referenced in the project navigator is not copied.
- Right-click the built
.appin Products in Xcode and choose Show in Finder, then Show Package Contents. The image should be visible at the top level (or inside its folder reference). - Images that live only in an asset catalog cannot be referenced by filename from HTML. An
.xcassetsentry is compiled intoAssets.car, not into a loose file. Copy the image into the bundle as a normal resource if HTML needs to link it. - Match the case exactly. The iOS file system is case-sensitive, so
myimage.pngwill not findMyImage.pngeven though the same markup works on a default macOS volume.
How to prevent it
- Always pass a base URL when loading HTML that references bundled assets — never
nil. - Keep
srcattributes relative and let the base URL do the work; do not hand-buildfile://strings. - Use
loadFileURL(_:allowingReadAccessTo:)for bundled HTML files onWKWebView, neverload(URLRequest:). - Add images to Copy Bundle Resources and keep filenames lowercase to dodge case-sensitivity surprises on device.
- New code should target
WKWebViewonly;UIWebViewis deprecated and will not pass review.
FAQ
Why does the image load in the Simulator but not on a real device?
The Simulator runs on your Mac’s default case-insensitive volume, while the device uses a case-sensitive file system. A mismatch like Image.png in the markup versus image.png in the bundle resolves on the Mac and fails on hardware. Rename the file or fix the src to match exactly.
Can I put an absolute file:// path in the src attribute?
Not reliably. Without a base URL the HTML has no origin, and file:// references are blocked as cross-origin. Pass the bundle directory as the base URL and use a relative path instead — it is shorter and it keeps working when the app’s container path changes between launches.
Does this apply to WKWebView as well?
Yes. loadHTMLString(_:baseURL:) behaves the same way, and for a bundled HTML file you must use loadFileURL(_:allowingReadAccessTo:) rather than a plain URLRequest. Once the base URL or read-access directory is correct, relative image paths resolve normally.
Parts of this guide are adapted from content licensed under CC BY-SA 4.0; this article is shared under the same license.
