8

I am using the delegate method shouldStartLoadWithRequest to catch link clicks and handle specific cases inside my app instead of allowing the webView to navigate to the link. In this code, I am attempting to push a new ViewController onto the stack. Immediately after the attempt to push the view, I get a crash with the following message in my console:

WebKit discarded an uncaught exception in the webView:decidePolicyForNavigationAction:request:frame:decisionListener: delegate:

My code looks like this:

-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    if(decision logic){
        MyViewController *vc = [[MyViewController alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
        [vc release];
        return NO;
    }

    return YES;
}

I have also tried using a modal instead of pushing a new viewController. I get the same result. Is there any other scenarios I should be handling here?

**Edit: Just thought of this. The view I'm trying to push contains another UIWebView. Should I be doing something to the first webView before transitioning? I just testing pushing a different view controller that doesn't contain a webView and it worked fine.

Cezar
  • 55,636
  • 19
  • 86
  • 87
Mark Struzinski
  • 32,945
  • 35
  • 107
  • 137

5 Answers5

8

I fixed this by ensuring that all previous requests on the webview were stopped before continuing:

[webview stopLoading];
Mark Struzinski
  • 32,945
  • 35
  • 107
  • 137
  • 3
    Hi Mark You seams to have fixed the issue with the childbrowser, I just wonder where to put the [webview stopLoading];? I have tested a couple of places with no luck, could you please tell me where to put the code? Thanks a lot! – Claes Gustavsson Jun 27 '12 at 08:12
  • I tried to put the [webView stopLoading] in the shouldStartLoadWithRequest method, just before returning NO. This did NOT solve my problem. @Mark: coud you please give us some more details? – Giorgio Barchiesi Feb 26 '14 at 09:59
  • I finally discovered that my problem was actually caused by setting to nil an array element, in a portion of code belonging to the new view controller being pushed, so it was not a problem with the web view controller itself. – Giorgio Barchiesi Feb 26 '14 at 10:09
6

I have absolutely no idea why I'm getting this error from Webkit, but I traced it down to trying to insert a value in a dictionary with a nil key.

olivaresF
  • 1,369
  • 2
  • 14
  • 28
  • Same issue here; trying to insert a value in a dictionary with a nil key throws the error "WebKit discarded an uncaught exception in the webView:decidePolicyForNavigationAction" – lucasart Jul 12 '12 at 03:56
  • Where were you inserting a nil key? in a delegate method (if so, which one)? – Joel Martinez Jul 26 '13 at 20:41
  • I'm not sure about this, as it was a year and a half ago, but from what I can remember, it was an absolutely unrelated dictionary in another class. – olivaresF Jul 26 '13 at 23:22
  • What a bizarre bug! In my case I was getting the warning `WebKit discarded an uncaught exception in the webView:decidePolicyForNavigationAction:request:frame:decisionListener: delegate: nil is not a valid predicate for filtering`. After seeing this answer, I realized the predicate in question was not remotely related to WebKit. – robotspacer May 16 '14 at 19:23
2

Check out if you nullify delegate property of UIWebView in its controller dealloc method (of course if this controller is webview's delegate). Like this:

- (void) viewDidLoad
{
    webView.delegate = self;
}
- (void) dealloc
{
    webView.delegate = nil;
}

From the Apple docs on the delegate property:

Important. Before releasing an instance of UIWebView for which you have set a delegate, you must first set its delegate property to nil. This can be done, for example, in your dealloc method.

Igor Vasilev
  • 356
  • 4
  • 6
  • Since I'm using ARC, and no dealloc method is allowed, I tried to nullify the delegate in the viewWillDisappear. – Giorgio Barchiesi Feb 26 '14 at 10:01
  • No, it's not a good place. Better the viewWillUnload, perhaps. Otherwise the delegate will not work any longer when popping the new view controller, thus returning to the web view controller. – Giorgio Barchiesi Feb 26 '14 at 10:13
  • 1
    Giorgio, you should still use your controller's dealloc method under ARC, you just mustn't call [super dealloc]. As about viewWillUnload, this method is deprecated since iOS6: controller root views are no longer unloaded on low memory conditions. – Igor Vasilev Mar 14 '14 at 13:24
1

Yet another cause for this error can be the fact that Autolayout is accidentally left enabled in the viewcontroller containing the webview. This happened to me while testing on iOS5.

briomusic
  • 63
  • 7
0

I got a similar error:

***WebKit discarded an uncaught exception in the webView:decidePolicyForNavigationAction:frame:decisionListner: delegate: - [UIPopoverController dealloc] reached while popover is still visible.

The problem seems to be caused by the following code:

UIPopoverController *pc = [[UIPopoverController alloc] initWithContentViewController:vc];
[pc presentPopoverFromRect:frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionLeft animated:YES];

Apparently, pc is being deallocated when the routine exits. I changed the code to make pc a property and WebKit stopped complaining.

JSWilson
  • 1,113
  • 1
  • 11
  • 28