Understanding WKWebView: Comparison with UIWebView, Usage, Issues, and Solutions
This article introduces WKWebView, compares it with the legacy UIWebView in terms of performance and memory usage, explains usage patterns, delegate protocols, JavaScript‑Native interaction, common pitfalls such as cookie handling, process crashes, caching, and offers practical solutions and best‑practice recommendations for iOS developers.
WKWebView is a browser component introduced after iOS 8 that offers a significant performance and memory‑usage improvement over the older UIWebView. This article explains why WKWebView is needed, how to use it, and the problems that may arise during development.
1. Introduction: Why WKWebView is Needed
Hybrid development that embeds web content in an app has become a hard requirement. UIWebView suffers from severe performance and memory consumption issues, limiting flexibility. WKWebView, based on a multi‑process architecture, Nitro JS engine, and high refresh rates, provides Safari‑level performance and accurate loading progress.
2. Usage: Comparison with UIWebView
Creating and loading a URL in WKWebView is similar to UIWebView:
UIWebView *webView = [[UIWebView alloc] init];
NSURL *url = [NSURL URLWithString:@"https://www.hujiang.com"];
[webView loadRequest:[NSURLRequest requestWithURL:url]]; WKWebView *webView = [[WKWebView alloc] init];
NSURL *url = [NSURL URLWithString:@"https://www.hujiang.com"];
[webView loadRequest:[NSURLRequest requestWithURL:url]];Both use a URL request, but WKWebView adds an extra navigation‑decision step after the server response, giving finer control.
Delegate Differences
WKWebView uses WKNavigationDelegate while UIWebView uses UIWebDelegate . The navigation flow includes additional callbacks such as decidePolicyForNavigationAction and decidePolicyForNavigationResponse , allowing the app to approve or reject loads.
#pragma mark - UIWebViewDelegate
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {return YES;}
#pragma mark - WKNavigationDelegate
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {}
- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler {}Other lifecycle callbacks (start load, finish load, fail load) also differ, with WKWebView providing more granular delegate methods.
JS ↔ Native Interaction
Native‑to‑JS calls are asynchronous in WKWebView:
#pragma mark - UIWebView
NSString *title = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];
#pragma mark - WKWebView
[wkWebView evaluateJavaScript:@"document.title" completionHandler:^(id _Nullable ret, NSError * _Nullable error) {
NSString *title = ret;
}];JS‑to‑Native communication uses WKScriptMessageHandler via a user content controller:
WKUserContentController *userContent = [[WKUserContentController alloc] init];
[userContent addScriptMessageHandler:self name:@"MyNative"];
WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
config.userContentController = userContent;
WKWebView *webView = [[WKWebView alloc] initWithFrame:CGRectZero configuration:config]; #pragma mark - WKScriptMessageHandler
- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message {}
// JavaScript
function callNative() {
window.webkit.messageHandlers.MyNative.postMessage('body');
}3. Known Issues and Solutions
Cookie Problems
WKWebView does not reliably write cookies to NSHTTPCookieStorage in real time, causing session loss after app restarts. Work‑arounds include manually reading cookies from responses and injecting them into subsequent requests, or abandoning cookie reliance in favor of OAuth2 authentication.
Process Crashes
When the WKWebView process crashes, the app may show a white screen. iOS 9 provides a delegate callback; on iOS 8 you can check if webView.title is empty to detect a crash and reload.
Caching and Data Store
WKWebView caches data internally. From iOS 9 onward you can clear caches via WKWebsiteDataStore :
// RemoveCache
NSSet *websiteTypes = [NSSet setWithArray:@[WKWebsiteDataTypeDiskCache, WKWebsiteDataTypeMemoryCache]];
NSDate *date = [NSDate dateWithTimeIntervalSince1970:0];
[[WKWebsiteDataStore defaultDataStore] removeDataOfTypes:websiteTypes modifiedSince:date completionHandler:^{}];On earlier iOS versions you must delete the cache directory manually.
Other Functional Issues
Redirect handling, custom URL schemes, download links, cross‑origin restrictions, and UIWebView‑specific behaviors need explicit handling in WKWebView delegates.
Adjusting content insets, handling keyboard resize events, and dealing with page navigation caching are also important for a smooth user experience.
Conclusion
Although WKWebView introduces new challenges such as cookie synchronization, process stability, and limited caching control, its performance benefits and modern web standards support make it the preferred web component for iOS apps. With proper handling of the outlined issues, developers can confidently adopt WKWebView as the mainstream solution.
Hujiang Technology
We focus on the real-world challenges developers face, delivering authentic, practical content and a direct platform for technical networking among developers.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.