UIWebView 第二次卡住了

UIWebView stuck at the second time

我正在开发一个 iOS 应用程序,用户可以从收件箱列表中选择一封电子邮件,并在下一页上查看它(有一个 UIWebView 来提供内容)。

这是我观察到的情况:我第一次选择电子邮件时,它很快就显示在第二页上。但是,如果我返回并选择再次查看电子邮件,则需要很长时间才能在第二页上调用 webViewDidFinishLoad:

我在控制台中看到以下消息:

void SendDelegateMessage(NSInvocation *): 
delegate (webView:didFinishLoadForFrame:) failed to return after waiting 10 seconds. main run loop mode: kCFRunLoopDefaultMode

打印此消息后,内容将立即呈现在 WebView 上。

我不明白的是,这总是发生在第二次使用 webview 显示 HTML 内容时。 files/resources 没有本地缓存​​,也没有网络问题。

请指出我应该在哪里查看此问题。谢谢!

----更新----

在我的随机尝试中,我发现删除了这一行:

self.webView.delegate = self;

将使问题消失。这让我感到更加困惑。为什么让self成为delegate会出现这样的问题?

---- 更新 2 ----

我发现罪魁祸首是我在 webview 加载 html 后调用的 flatten html 方法。 如果我删除 NSHTMLTextDocumentType,一切都很好,但我不明白为什么:

+ (NSString *) stripHtmlSchema: (NSString *)htmlString
{
    NSString *flatString = [[[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUTF8StringEncoding]
                                                            options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
                                                                      NSCharacterEncodingDocumentAttribute: [NSNumber numberWithInt:NSUTF8StringEncoding]}
                                                 documentAttributes:nil
                                                              error:nil] string];
    return flatString;
}

---- 最后更新----

解决了问题。 [NSAttributedString alloc] initWithData 执行时间太长,因此阻止了所有内容。

解决了问题。 [NSAttributedString alloc] initWithData 执行时间太长,因此阻止了所有内容。