views:

199

answers:

2

I'm trying to implement a UIWebViewDelegate in my application and I cannot for the life of me get it to work. I would really appreciate a second set of eyes on this.

I know the name MapViewController is confusing, but the WebView is controlling a map (not UIMapView).

Here's the bulk of the code:

MapViewController.h

#import <UIKit/UIKit.h>
#import <UIKit/UIWebView.h>

@interface MapViewController : UIViewController<UIWebViewDelegate> {
    IBOutlet UIWebView *webView;
}

@property (nonatomic, retain) IBOutlet UIWebView *webView;

@end

MapViewController.m

- (void)viewDidLoad {
    [super viewDidLoad];
    webView.delegate = self;
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"map" ofType:@"html"]isDirectory:NO]];
    [webView loadRequest:request];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView{
    NSLog(@"Done loading.");
}
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
    NSLog(@"hi");
    return NO;
}
- (void)dealloc {
    webView.delegate = nil;
    [webView release]
    [super dealloc];
}

Thanks for any help!

A: 

Does the initial page you're loading actually display? Are you sure you connected your webview in Interface Builder?

Matt Long
Yea, everything loads just fine. My HTML file has a handful of links and I want to use objective-C to handle what happens when the links are clicked.
Daniel
You could try to set the delegate in IB instead of in code. See if that makes any difference.
Matt Long
Does your delegate class implement UIWebViewDelegate, btw?
Matt Long
Isn't that what @interface MapViewController : UIViewController<UIWebViewDelegate> does in the header file?
Daniel
Yep. Sorry. Missed that in your initial code. My bad. This should be working. What happens when you pass in something like @"http://www.google.com" for your URL? Does your delegate get called then?
Matt Long
Nope. It loads google.com just fine but nothing ever appears in the log.
Daniel
Just noticed that you're returning NO in -shouldStartLoadWithRequest:. When you do that, -webViewDidFinishLoad will never get called. Does your log message "hi" ever display? Meanwhile, try this example project: http://www.cimgf.com/files/WebViewThing.zip
Matt Long
May I suggest you to take 5-10 minutes to do up a fresh UIWebView in a new project? There may well be something you missed/messed in IB if you're relatively new to this.
William
Thanks Matt for your example and thanks William for the suggestion. I ended up just redoing the project and it worked this time. Not even 100% sure what I did differently!
Daniel
A: 

Things to check:

  1. if map.html is in the bundle. Put it on the root of the bundle, not on resources.
  2. if it is, right click on it, and choose get info. Verify on TARGETS if the target is checked for the file.
Digital Robot
Thank you, but I should have clarified:The web view loads the web page just fine. What I'm wanting is to use webView:shouldStartLoadWithRequest:navigationType to pass arguments from JavaScript to Objective-C.
Daniel