iOS 8
WKWebView
local files
troubleshooting
web development

WKWebView not loading local files under iOS 8

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction to WKWebView and Local File Loading Issues

In iOS 8, Apple introduced WKWebView as a replacement for UIWebView to provide enhanced performance, better standards compliance, and improved resource management for rendering web content in iOS applications. Despite these advantages, developers transitioning to WKWebView from UIWebView encountered an unexpected limitation: difficulty loading local files.

This article explores the reasons behind this issue, examines potential workarounds, and provides guidance on best practices for working with WKWebView under iOS 8.

Understanding the WKWebView Architecture

WKWebView was designed to offer improved performance by offloading rendering processing from the app's main process to a separate web content process. This approach isolates the web content and enhances the security and stability of iOS applications.

However, this architectural change also came with new security restrictions that affect how local files are accessed. Under WKWebView, local file paths are sandboxed, which prevents direct access to files stored within the app’s bundle or documents directory.

Key Challenges

1. Sandbox Restrictions

Sandboxing restricts direct file access by limiting the WKWebView's ability to load files from arbitrary file paths that were previously supported by UIWebView. The main security benefit is to prevent potential malicious access to files, but it also impedes legitimate uses such as loading HTML or other resources from the app’s bundle.

2. Content Security Policy (CSP)

WKWebView enforces strict Content Security Policy (CSP) settings by default, which hampers loading of local files. Developers need to explicitly permit access to these resources, adding additional overhead to web content management within an app.

3. Scheme Limitations

WKWebView disallows the use of custom URL schemes for loading files, which means developers can't simply register their own schemes to bypass the security limitations as a workaround, which was possible in UIWebView.

Workarounds for Loading Local Files

1. Embedded HTTP Server

One of the popular solutions is running a local HTTP server within the app to serve the local files via HTTP/S. This method bypasses the `file://` URL restriction:

  • Prefetching Resources: Optimize the loading of web resources by preloading them into the local storage when online, reducing reliance on local files.
  • Use of WKWebViewConfiguration: Customize the `WKWebViewConfiguration` to manage cookies, custom schemes, and scripts.
  • Security: Ensure that the use of local resources does not introduce vulnerabilities by regularly auditing your code for security risks.

Course illustration
Course illustration

All Rights Reserved.