Under the Bridge

UIWebView internals

As you’ve no doubt noticed if you’ve tried to do anything even mildly different with UIWebView — oh, like, just to take a completely random sample, wanting to call -setCustomUserAgent: on it for the convenience of the server your application is presenting content from — the public interface is very limited, and WebKit/WebView.h is a private framework, so no SDK-compliant coding for YOU!

However, if you’re not overly concerned with the strictest adherence possible to the rules, here’s a fellow who’s worked out how to get at the internal WebView, for purposes of presenting a progress bar:

So, say you have a UIWebView called officialSDKWebView. We want to extract the main WebView from that to get the progress. To do so, we have to use another internal class called UIWebDocumentView. From there, we can get the WebView easily. Here’s the code:

UIWebDocumentView *documentView = [officialSDKWebView _documentView];
WebView *coreWebView = [documentView webView];

There’s a couple ah, non-standard header files required, and maybe a bit of other messing about in the project, but it’s all provided in that post. And once you follow it, you do appear to indeed have a genuine WebKit WebView that you can call anything from your desktop code on. Like, in the case of our completely random sample above,

[coreWebView setCustomUserAgent:@"I'm an iPhone!"];

Ex-cellent.

Of course, we would never actually recommend that you violate the published SDK guidelines, that is unsafe and irresponsible, yada yada yada. That said, relying on the iPhone SDK’s WebView to have its internals remain consistent with the desktop SDK’s … that is, most likely, a pretty darn safe bet.

h/t: iPhoneKicks!

2