Javascript console.log in an iOS UIWebView
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
UIWebView is legacy technology, but many older iOS codebases still contain it. One of the biggest debugging frustrations is that JavaScript console.log output is not surfaced the way it is in a desktop browser. The usual workaround is to override console.log in JavaScript and forward the message to native code.
Why console.log Is Awkward in UIWebView
UIWebView predates the better debugging tools that later arrived with WKWebView. In a legacy app, JavaScript runs inside the embedded web view, but the standard browser console is not readily exposed to you in the app UI.
That means console.log("hello") may execute, but you do not see the output anywhere useful.
Bridge console.log to Objective-C
A classic workaround is:
- inject JavaScript that replaces
console.log - make the replacement trigger a custom URL scheme
- intercept that request in the
UIWebViewDelegate - print the message in native code
The injected JavaScript can look like this:
When console.log runs, it navigates to a fake URL beginning with js-log:. Native code can intercept that navigation and stop it.
Intercept the Log Message in UIWebViewDelegate
In Objective-C, implement webView:shouldStartLoadWithRequest:navigationType:.
Now JavaScript log calls appear in the Xcode console through NSLog.
Inject the Override After the Page Loads
Once the page finishes loading, inject the JavaScript override from native code.
After this, a script in the page such as:
will produce native console output.
Handle Multiple Arguments More Carefully
console.log often receives multiple arguments, not just one string. If you want better output, join all arguments before forwarding them.
That handles calls like console.log("user", userObject) more cleanly.
Limitations of the URL-Scheme Technique
This bridge is useful, but it is still a workaround. The custom URL approach has limits:
- it is noisy if the page logs frequently
- it can interfere with navigation if implemented carelessly
- large messages may be truncated or awkward to encode
- it is only suitable for debugging, not production telemetry
For serious web-view debugging, WKWebView is the better platform.
Prefer WKWebView for New Work
If you can migrate, do it. WKWebView has better performance, a cleaner JavaScript bridge, and more modern inspection options. For legacy maintenance, the console.log override pattern is still practical, but it should be treated as a compatibility technique rather than a long-term design.
Common Pitfalls
Forgetting to return NO after intercepting the fake js-log: navigation. Then the web view tries to load the fake URL.
Injecting the override too early. Wait until the page has loaded or your script may not attach correctly.
Logging non-string objects without serialization. Convert them explicitly for readable output.
Using this as a production logging mechanism. It is fine for debugging, not for structured telemetry.
Ignoring the larger issue that UIWebView is deprecated. Use WKWebView for active development.
Summary
- '
UIWebViewdoes not expose JavaScript logs conveniently, soconsole.logoften appears invisible.' - A common workaround is to override
console.logand forward messages through a custom URL scheme. - Intercept that scheme in the
UIWebViewDelegateand print withNSLog. - This is useful for legacy debugging, but
WKWebViewis the better long-term solution. - Keep the bridge simple and debugging-focused so it does not interfere with navigation.
Related reading
- Javascript data structures library
- JavaScript DEFLATE Implementation
- Javascript equivalent of Python's zip function
- JavaScript generator-style async
- Jerky Scrolling After Updating UITableViewCell in place with UITableViewAutomaticDimension
- Jerky Scrolling After Updating UITableViewCell in place with UITableViewAutomaticDimension
- Javax validation on nested objects - not working
- javax.management.InstanceNotFoundException org.springframework.boottypeAdmin,nameSpringApplication
.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.