重用 UIWebView 会导致崩溃

发布于 2025-01-04 11:43:55 字数 773 浏览 3 评论 0原文

我读到重用 UIWebViews 是一种不好的做法。我在工作中继承的一些代码将各种内容推送到 UIWebView。 Powerpoint、Word 文档和视频。这一切在正常情况下都可以正常工作。当我们太快地切换 UIWebView 中的内容时,它就会转储。

我的 webView 设置为属性。它在IB中连接得很好。从我们的 tableView 中正常选择加载本地内容就可以了。它需要快速选择同一个单元或多个单元的组合才能使其崩溃。

我可以为 webViewDidFailWithError 捕获一些错误消息。但即使没有崩溃,这些也会触发。 这是错误本地化字符串。

The operation couldn’t be completed. (NSURLErrorDomain error -999.)

当应用程序最终崩溃时,它会因为这个愚蠢的 WebCore 错误而崩溃。

应用程序崩溃图像

如果有人有任何链接或一些代码示例如何处理此问题,我将不胜感激。也许是一个如何最好地重用我的 webView 属性而不破坏事情的例子。

我会加载一些代码,但是有很多事情与 webView 本身无关。所有推送到 webView 的内容都是通过 [self.webView loadRequest:request]; 完成的,请求是一个 NSURLRequest,其中填充了本地内容的路径。

如果有人能帮助我解决这个问题,我将非常感激。为简单的事情祈祷。

I've read that reusing UIWebViews is kind of a bad practice. Some code I inherited at work is pushing a variety of content to a UIWebView. Powerpoints, Word documents, and video. This all works just fine under normal circumstances. When we get to switching out the content in the UIWebView too fast, it dumps.

My webView is set as a property. It is hooked up in IB just fine. Normal selection from our tableView loads local content just fine. It takes rapid fire selection of either the same cell or combinations of multiple to get it to crash.

I can capture some error messages for it for the webViewDidFailWithError. But those will trigger even without a crash.
Here is the error localized string.

The operation couldn’t be completed. (NSURLErrorDomain error -999.)

When the app does finally crash, it blows up on this goofy WebCore error.

Application Crash image

If anyone has any links or some code examples how to handle this I would appreciate it. Maybe an example how to best reuse my webView property without blowing things up.

I would load some of my code, but there is a lot going on not related to the webView itself. All content being pushed to the webView is done via [self.webView loadRequest:request]; with the request being an NSURLRequest filled with the path to the local content.

I will be very appreciative if anyone can help me out on this one. Fingers crossed for something simple.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

旧竹 2025-01-11 11:43:55

我不确定这种方式是解决问题的“最佳”方式,但它似乎确实运作良好。简短、甜蜜,而且有效。

我禁用了与 tableView 的 userInteraction 来更新 webView 中的内容。由于你必须如此频繁地捣碎它,因此这里或那里错过一个点击可能不会错过。所以现在,这就是解决办法。

#pragma mark -
#pragma mark UIWebViewDelegate methods

-(void)webViewDidStartLoad:(UIWebView *)webView {
    [self.tableView setUserInteractionEnabled:NO];
}

-(void)webViewDidFinishLoad:(UIWebView *)webView {
    [self.tableView setUserInteractionEnabled:YES];
}

// I re-enable on load failures as they can block you out entirely on fail
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
    [self.tableView setUserInteractionEnabled:YES]; 
}

I'm not certain this way is the "best" way to solve the issue, but it does seem to be working quite well. Short, sweet, and it works.

I disabled userInteraction with the tableView that updates the content in the webView. Since you have to mash on it so much, missing a tap here or there probably won't be missed. So for now, this is the fix.

#pragma mark -
#pragma mark UIWebViewDelegate methods

-(void)webViewDidStartLoad:(UIWebView *)webView {
    [self.tableView setUserInteractionEnabled:NO];
}

-(void)webViewDidFinishLoad:(UIWebView *)webView {
    [self.tableView setUserInteractionEnabled:YES];
}

// I re-enable on load failures as they can block you out entirely on fail
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
    [self.tableView setUserInteractionEnabled:YES]; 
}
风月客 2025-01-11 11:43:55

如果它仅在您快速点击时崩溃,您可以在其上放置一个手势识别器来充当穷人的速率限制器吗?

If it’s crashing only when you tap quickly, could you put a gesture recognizer over it to act as a poor man's rate limiter?

伴随着你 2025-01-11 11:43:55

您在加载另一个请求之前是否使用 [webview stopLoading]; ?您可能需要做的是在尝试加载其他加载之前取消或停止当前加载。另一个选项是限制用户输入。

Are you using [webview stopLoading]; before loading another request? What you might need to do is cancel or stop the current loading before trying to load a different one. The other option being restrict the user input.

×眷恋的温暖 2025-01-11 11:43:55

在 UIWebViewDelegate 中,如果已经有请求加载,您可以实现 webView:shouldStartLoadWithRequest:navigationType: 来返回 NO

In your UIWebViewDelegate, you could implement webView:shouldStartLoadWithRequest:navigationType: to return NO if there is already a request loading.

从﹋此江山别 2025-01-11 11:43:55

我忍不住认为你最好不要重新使用 UIWebView。放弃笔尖,以编程方式创建它并将其设置为 nil,并在数据源更改时重新创建/重新分配/重新分配它。

另一方面,我会确保使用 NSOperationQueue 进行数据加载。

I can't help but think you'd be better off not re-using the UIWebView. Ditch the nib, create it programmatically and set it to nil and re-create/re-assign/re-alloc it when the data source changes.

On a different note I would make sure to use NSOperationQueue for the data loading.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文