iOS 5 UIWebview 委托:WebKit 在 webView:decidePolicyForNavigationAction 中丢弃了一个未捕获的异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7883899/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 22:02:05  来源:igfitidea点击:

iOS 5 UIWebview Delegate: WebKit discarded an uncaught exception in the webView:decidePolicyForNavigationAction

iosuiwebview

提问by Mark Struzinski

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

我使用委托方法shouldStartLoadWithRequest来捕获链接点击并处理我的应用程序内的特定情况,而不是允许webView导航到链接。在这段代码中,我试图将一个新的ViewController推入堆栈。在尝试推送视图后,我立即在控制台中崩溃并显示以下消息:

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?

我也尝试过使用模态而不是推送新的viewController. 我得到相同的结果。我应该在这里处理任何其他情况吗?

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

**编辑:刚刚想到这一点。我试图推送的视图包含另一个UIWebView. 我应该webView在过渡之前对第一个做些什么吗?我只是测试推送一个不包含 a 的不同视图控制器,webView它工作正常。

采纳答案by Mark Struzinski

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

我通过确保在继续之前停止 webview 上的所有先前请求来解决此问题:

[webview stopLoading];

回答by olivaresF

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.

我完全不知道为什么我会从 Webkit 收到此错误,但我将其追溯到尝试使用 nil 键在字典中插入一个值。

回答by Igor Vasilev

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:

检查您是否在其控制器 dealloc 方法中取消了 UIWebView 的委托属性(当然,如果此控制器是 webview 的委托)。像这样:

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

From the Apple docs on the delegate property:

来自委托属性的 Apple 文档:

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.

重要的。在释放已为其设置委托的 UIWebView 实例之前,您必须首先将其委托属性设置为 nil。例如,这可以在您的 dealloc 方法中完成。

回答by briomusic

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.

此错误的另一个原因可能是 Autolayout 在包含 webview 的视图控制器中意外启用。我在 iOS5 上测试时发生了这种情况。

回答by JSWilson

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.

***WebKit 在 webView:decidePolicyForNavigationAction:frame:decisionListner: delegate: - [UIPopoverController dealloc] 中丢弃了一个未捕获的异常,而 popover 仍然可见。

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.

显然,当例程退出时,pc 正在被释放。我更改了代码使 pc 成为一个属性,WebKit 停止抱怨。