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
Migrating from UIWebView to WKWebView is not just a find-and-replace exercise. WKWebView has a different architecture, different delegate APIs, and more explicit handling for JavaScript, cookies, and message passing. The good news is that the migration is usually straightforward once you map the old responsibilities to the newer WebKit APIs.
Why the Migration Matters
UIWebView is deprecated and removed from modern iOS development workflows. WKWebView is the supported replacement because it is faster, safer, and more feature-rich.
A few important differences:
- rendering happens in a separate process
- JavaScript evaluation is asynchronous
- navigation and UI delegates are more explicit
- script message handling is built in
Those differences improve behavior, but they also mean old UIWebView assumptions usually need cleanup.
Creating a WKWebView
A basic WKWebView setup looks like this:
If your old code created a UIWebView, loaded a request, and implemented delegate callbacks, this is the first shape of the replacement.
Delegate Mapping Changes
Many UIWebViewDelegate responsibilities move into WKNavigationDelegate and WKUIDelegate.
If the old code used shouldStartLoadWith, the equivalent decision point is decidePolicyFor navigationAction.
JavaScript Is Asynchronous Now
One of the biggest migration differences is JavaScript execution. stringByEvaluatingJavaScript from UIWebView was synchronous. WKWebView uses evaluateJavaScript with a completion handler.
That changes control flow. Code that assumed an immediate result now needs to continue inside the completion block or use higher-level async orchestration.
JavaScript-to-Native Messaging
WKWebView has a stronger bridge model through WKUserContentController.
Then implement the handler:
This is usually cleaner than the hacks older apps used with custom URL schemes.
Cookies and State Need Attention
WKWebView manages web data differently from UIWebView. If the app relied on shared cookies or authentication state, test that path carefully. In modern WebKit, cookie handling usually involves WKWebsiteDataStore and WKHTTPCookieStore rather than assuming the old global storage behavior.
That is one of the most common reasons a migration compiles but still breaks login flows.
Common Pitfalls
- Replacing the view type but forgetting to migrate old delegate logic to
WKNavigationDelegateorWKUIDelegate. - Assuming JavaScript evaluation is synchronous when
evaluateJavaScriptis asynchronous. - Keeping old custom URL-scheme bridges instead of using script message handlers.
- Migrating basic page loads successfully but failing to test cookies, authentication, and cached web state.
- Treating
WKWebViewas a drop-in replacement even though its configuration and lifecycle are more explicit.
Summary
- '
WKWebViewis the supported replacement forUIWebViewand has a different architecture.' - Create it with a
WKWebViewConfigurationand wire the correct delegates. - Update navigation decisions and load callbacks to the
WKNavigationDelegatemodel. - Migrate JavaScript code to
evaluateJavaScriptand script message handlers. - Test cookies and authentication flows carefully because web state handling changed.

