8
votes

I have a "product details" UIViewController on my storyboard which has a UIWebView to display "rich text" (basically bold and italic). The web view is loaded using a string (fetched from CoreData) loadHTMLString. The problem is that the first time the view is displayed, there is at least a one second delay before the web view is rendered. Further "product details" views that are loaded all load fine. It is only the first "product details" web view which takes the time to load.

This is what I have tried (with no joy)

  1. Displaying a UIWebView with text on the first screen (to warm up the UIWebView)
  2. When it comes time to show the details view, init the web view in the prepare for segue method and pass it to the "product details" view controller.
  3. I have tried various combinations of having the web view in the storyboard / not in the storyboard / in a scroll view etc

I would be very interested if others have had this problem and how they got over it / worked around it. I find it strange I have not been able to find many similar problems on SO; I must be doing something stupid...


Note, I am using a UIWebView so I can display rich text. I understand I can display rich text using other controls, but the program that makes the text that I want to display can only export rich text using HTML.

3
Have you tried preloading your Core Data-fetched HTML string? It could be that the slow component is Core Data, not the UIWebView. (Also, rather than guess, consider running your app under Instruments - it should tell you where the time is going.)Tim
You might be interested in projects like DTCoreText which allow you to parse HTML into something you can display without the heavy-weight and slow-to-load webview.Jesse Rusak
Tim, I have tried your suggestion (but thanks). @JesseRusak, I will take a look at DTCoreText. Thankslindon fox
I have the same problem, but without using Core Data. I read my html string with stringWithContentsOfFile:, and it is also slow the 1st time.Reinhard Männer

3 Answers

6
votes

This is a common problem for UIWebView. My workaround is to make the web view initially hidden (showing some sort of "Loading..." indicator instead), then make it un-hidden in the webViewDidFinishLoad: method.

3
votes

Instead of use an UIWebView to display rich text content, you can try use OHASBasicHTMLParser of OHAttributedLabel which is backwards compatible with versions prior to iOS 6.

You can find an example of its markup parsers here.

0
votes

This should help with the loading considerably:

    var data: Data? = htmlString.data(using: .utf8)

    if let aData = data {

        webView.load(aData, mimeType: "text/html", textEncodingName: "UTF-8", baseURL: NSURL() as URL)
    }

Credit: http://iphonedevsdk.com/forum/iphone-sdk-development/23481-why-the-load-delay-with-a-uiwebview-and-local-text.html