Migrating from UIWebView to WKWebView
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
UIWebView is deprecated, and WKWebView is the maintained replacement for embedded web content on Apple platforms. The migration is not just a class rename: WKWebView has a different process model, different delegation points, and different behavior for JavaScript, cookies, and local files.
Start with a Real WKWebView Configuration
The usual migration path is to build the web view in code, attach a WKWebViewConfiguration, and then move any existing delegate logic into the WKNavigationDelegate or WKUIDelegate APIs.
If your old code used outlets, that is still possible, but many teams find it easier to migrate in code first so configuration is explicit.
Update JavaScript Interaction and Messaging
A common UIWebView pattern was calling synchronous JavaScript evaluation. WKWebView uses asynchronous evaluation instead, which means you need to move follow-up logic into a completion handler.
For page-to-native communication, use script message handlers instead of ad hoc URL interception:
That approach is cleaner than parsing fake navigation requests and makes the bridge contract explicit.
Local Files, Cookies, and Storage Behave Differently
This is where many migrations fail. WKWebView does not behave exactly like UIWebView for local content or shared state.
If you are loading bundled HTML, use the file-loading API that grants read access to a directory:
That is important because a local page may need permission to read related assets such as JavaScript, CSS, or images from the same directory tree.
Cookies also need attention. WKWebView uses website data stores rather than behaving like the old shared UIWebView path. If your app depends on session cookies, logins, or injected headers, verify that behavior explicitly during migration.
For custom request logic, you may need to move code that once lived in shouldStartLoadWith into:
- '
WKNavigationDelegatedecisions' - custom URL scheme handling
- server-side changes that remove fragile client interception
Migration Strategy That Reduces Risk
Do not swap classes and hope for the best. Migrate feature by feature:
- Replace the view and basic loading.
- Rewire navigation delegates.
- Rebuild JavaScript bridging with message handlers.
- Test login, cookies, downloads, popups, and local assets.
- Remove every remaining
UIWebViewsymbol from the project and dependencies.
That last step matters. Even if your own code no longer uses UIWebView, an old third-party library can still block release readiness.
Common Pitfalls
The biggest pitfall is assuming delegate methods translate one for one. UIWebView and WKWebView expose similar concepts, but the timing and APIs differ.
Another frequent problem is local file loading. A page can appear to load while its scripts or styles silently fail because read access was not granted to the containing directory.
Teams also get tripped up by JavaScript timing. Since evaluateJavaScript is asynchronous, code that used to assume an immediate result must be restructured.
Finally, test authentication carefully. Cookie and storage behavior often differ enough to break an existing sign-in flow even when normal page rendering looks fine.
Summary
- '
WKWebViewmigration is an architectural update, not a search-and-replace.' - Move loading and navigation logic into
WKWebViewConfigurationand delegates. - Replace synchronous JavaScript assumptions with completion handlers.
- Use
loadFileURL(_:allowingReadAccessTo:)for bundled local content. - Re-test cookies, authentication, and third-party dependencies before shipping.

