views:

288

answers:

1

Hi,

I'm having trouble intercepting URL clicks within a UIWebView. I've read around and found the most common problem is that links have a target="_blank" attribute to open in a new window, but mine have no such attribute. URLs are of the form "/word", where word is some arbitrary word. I'm also encoding them with %20 when necessary. My UIWebViewDelegate class doesn't even receive a shouldStartLoadWithRequest: event, just nothing happens. I've also tried inserting a button into the HTML, and these are unresponsive too.

Are there any other rules governing which URLs UIWebView will acknowledge? I've tried using an absolute URL (and even just "http://www.google.com"), and also replacing the entire string of HTML with just a valid link to google, but all to no avail. And the really odd thing is that I can find one link that will work - it can be clicked and I successfully catch the event, but there seems (I've looked pretty hard) to be nothing different between the HTML containing this link and the others.

Given that I'm new to the iPhone platform, I figure I may very well be doing something more fundamentally wrong. Flow through the app looks like this: the main view is a UITableViewController, which lets users select from a list of words. When a word is selected, a new UIViewController is created and pushed onto the screen. This is the view with a UIWebView, which displays data loaded from a SQLite database. Some relevant bits of code are below.

//move from the UITableViewController to UIViewController with UIWebView
DetailViewController *viewController = [[DetailViewController alloc] initWithNibName:@"DetailView" bundle:nil];
...
[self.navigationController pushViewController:viewController animated:YES];

//load HTML and display it in a UIWebView
NSString *output = [NSString stringWithFormat:@"%@%@%@",header,definition,footer];
[definitionView loadHTMLString:output baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]];

Any help would be greatly appreciated - been pulling my hair out for a while with this one.

A: 

You should take a look at PhoneGap: http://phonegap.com/

PhoneGap does exactly this (basically it's a big giant UIWebViewDelegate that makes it possible to write JavaScript apps for the iPhone that are able to take advantage of the camera, GPS, accelerometer etc.). Download the source, open PhoneGap.xcodeproj in the 'iphone' folder, and take a look at class PhoneGapDelegate. This is a great example usage of UIWebViewDelegate, hard to find a better example :) And it works of course.

Zoran Simic