iOS how can I add a completion block to UIWebView loadRequest?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
UIWebView's loadRequest(_:) method does not have a built-in completion handler. It fires off the request and returns immediately. To detect when loading finishes, you implement the UIWebViewDelegate protocol and respond to webViewDidFinishLoad(_:) and webView(_:didFailLoadWithError:). You can wrap this delegate pattern in a completion block by storing a closure and calling it from the delegate methods. However, UIWebView was deprecated in iOS 12 — for new code, use WKWebView which has native completion handler support.
The Problem
loadRequest starts an asynchronous load but provides no callback. You cannot chain actions after the page loads without additional plumbing.
Solution with UIWebViewDelegate
The delegate methods are called by UIWebView when loading completes or fails. This is the standard pattern for pre-iOS 12 code.
Wrapping in a Completion Block
This wrapper stores the completion closure and calls it from the delegate methods. Setting completion = nil after calling it prevents double invocation.
Better Approach with WKWebView (Recommended)
WKWebView replaced UIWebView starting in iOS 8 and is the only supported option from iOS 12 onwards. It uses WKNavigationDelegate for load callbacks and runs content in a separate process for better performance and security.
WKWebView with evaluateJavaScript Completion
WKWebView provides completion handlers for JavaScript evaluation. For load completion, you still use the navigation delegate pattern, but the API is more modern and reliable.
Async/Await with WKWebView (iOS 15+)
iOS 15 added async/await support to WKWebView, eliminating the need for delegate callbacks entirely in modern code.
UIWebView to WKWebView Migration
| Feature | UIWebView | WKWebView |
| Load completion | Delegate only | Delegate + async/await |
| JavaScript | stringByEvaluatingJavaScript (sync) | evaluateJavaScript (async with completion) |
| Process | In-app process | Separate WebContent process |
| Performance | Slower, more memory | Faster, efficient |
| Status | Deprecated iOS 12 | Current, supported |
| App Store | Rejected since 2020 | Required |
Common Pitfalls
- Using UIWebView in new projects: Apple rejects App Store submissions containing
UIWebViewreferences since April 2020. Always useWKWebViewfor new development. - Multiple loadRequest calls with one completion: If you call
loadRequestagain before the first load finishes, the stored completion block gets overwritten. Queue requests or cancel the previous load first. - Forgetting to set delegate before loadRequest: If
webView.delegateis not set before callingloadRequest, the delegate methods never fire. Always assign the delegate before initiating the load. - Retain cycles with completion closures: Storing
selfin the completion closure while the web view holds a strong reference to the delegate creates a retain cycle. Use[weak self]in closures to break the cycle. - Assuming webViewDidFinishLoad fires once: A single page load may trigger
webViewDidFinishLoadmultiple times due to iframes, redirects, or embedded resources. Track the main frame load separately if you need a single completion event.
Summary
UIWebView.loadRequesthas no completion handler — useUIWebViewDelegatemethods- Wrap delegate callbacks in a stored closure for a completion-block API
UIWebViewis deprecated since iOS 12 and rejected from the App Store since 2020- Use
WKWebViewwithWKNavigationDelegatefor modern load completion handling - iOS 15+ supports async/await on
WKWebViewfor the cleanest completion pattern - Always use
[weak self]in completion closures to avoid retain cycles
Related reading
- iOS How does one animate to new autolayout constraint height
- iOS How to debug freshly launching an app from a URL
- iOS How to detect if a user is subscribed to an auto-renewable subscription
- iOS How to get a proper Month name from a number?
- iOS how to perform a HTTP POST request?
- iOS How to run a function after Device has Rotated Swift
- iOS How to run a function after Device has Rotated Swift
- iOS how to set app icon and launch images
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.