3

I've started using shouldStartLoadWithRequest and been encountering puzzling behaviour as I understand things. In it's most simplest form I've tried the following...

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType 
{
  return YES;
}

With the side effect of when I click on HTML links the links do not load new pages in the UIWebView. Very likely I am not understanding something completely here. Any feedback/help would be greatly appreciated.

Rob Segal
  • 7,515
  • 12
  • 43
  • 72

1 Answers1

8

Two Possible reason

1> either you have not set the delegate Set delegate your webview to your class i.e in viewDidLoad method

webView.delegate = self; 

(if webView is taken in xib file you need to set the delegate from sib file)

2> your class does not implement UIWebViewDelegate protocol i.e. in your interface's declaration you haven,t declare it like this

@interface RootViewController : UIViewController<UIwebViewDelegate>
Mihir Mehta
  • 13,743
  • 3
  • 64
  • 88
  • 1
    While it wasn't the exact direct cause #1 did lead me to the issue. I was setting the delegate for the UIWebView in a xib file as well as in code. – Rob Segal Jan 18 '11 at 02:55
  • When I took the webView.delegate = self call out from where I was using the UIViewController that contained the UIWebView all was well. This way as you have mentioned only the xib file is responsible for setting the delegate. Thanks for the answer. – Rob Segal Jan 18 '11 at 03:13
  • So it turns out I haven't solved this problem just yet. I tried to simplify my situation by using a straight UIWebView instance in code instead of using a UIWebView through a .xib file but no matter what I put in as the return value in the shouldStartLoadWithRequest callback (i.e. yes or no) links don't seem to be followed. The strange thing is that if I leave out my shouldStartLoadWithRequest callback completely all links work fine. – Rob Segal Jan 19 '11 at 01:35
  • Ok my mistake. I did solve it for real this time. The problem was that I am using a custom URL scheme for my application. That in itself is not the issue but it certainly caused confusion for me. Initially I had code in the "handleOpenURL" callback in the application delegate to handle these custom urls. That was working great but when I added a "shouldStartLoadWithRequest" callback to my class which contains the UIWebView it starting handling those custom URL page loads because the HTML in my UIWebView contained those custom URLs. – Rob Segal Jan 19 '11 at 05:38
  • That's alot of description so I hope it makes some sense. Certainly I'm pleased that things are working properly now. – Rob Segal Jan 19 '11 at 05:42
  • "if webView is taken in xib file you need to set the delegate from sib file" - how can I set the delegate from xib file? – Lior Frenkel Mar 30 '11 at 20:35
  • sorry. that was a stupid question :) just connecting delegate of the uiwebview to its view :) – Lior Frenkel Mar 30 '11 at 20:37