The UIWebView class and its companion UIWebViewDelegate protocol have just a handful of methods -- far fewer than most UIKit classes -- but don't let their basic appearance fool you.
Malicious apps masquerading as a souped-up web browser can extract passwords and other sensitive information off the web page you are on and send this data to an unauthorized third party. (That's why I generally use only the Safari web browser that comes with my iPhone/iPad to surf the web. You should too.)
On the other hand, non-malicious apps (like my Stupid Browser app) can take advantage of this awesome feature to thoroughly customize your web browsing experience. Let's see how all this works.
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"lolcat" ofType:@"js"];
NSData *fileData = [NSData dataWithContentsOfFile:filePath];
NSString *jsString = [[NSString alloc] initWithData:fileData encoding:NSUTF8StringEncoding];The tricky part is remembering that when you drag a .js file into Xcode, it may be added to the Compile Sources section of your app target, and you will get a mysterious compilation error when you try to compile. If this happens, simply drag your .js file out of Compile Sources into Copy Bundle Resources where it belongs.
In Stupid Browser, I used something like the following:
The UIWebView calls its delegate (an object that implements the UIWebViewDelegate protocol) several times over the course of loading content from the web. The delegate method we care most about is webView:shouldStartLoadWithRequest:navigationType:.
The code snippet:
A few gotchas
I used two techniques in Stupid Browser to get around this issue:
To see all this in action, check out my Stupid Browser app on the app store.