8

I have an application that uses UIWebViews in several view controllers. The UIWebViews are used to render locally generated html, no slow network access required.

To save memory I only load these on demand as prompted by the viewcontroller viewWillAppear callback. (And unload offscreen instances in response to didReceiveMemoryWarning messages.)

The problem is that the user gets to see the html being rendered, sometimes accompanied by flashes of styling and other assorted unpleasant artifacts. I would much rather the rendering be done offscreen, and reveal the fully rendered view when its ready.

It would be very tidy to be able to have the viewWillAppear not return until the UIWebView is fully rendered. But how?

I tell the UIWebView what to render by sending it a loadHTMLString:baseURL: message. This is asynchronous, and some time (soon) later the webview's delegate gets called back webViewDidFinishLoad.

I experimented with running a runloop inside viewWillAppear, running either the NSDefaultRunLoopMode or UITrackingRunLoopMode. This works in the simulator (it complains to the log

[CATransaction synchronize] called within transaction

but does work) but on a device it deadlocks, with webViewDidFinishLoad never being called.

(Also, it seems like the UIWebView loading property doesn't work. At least, after I call loadHTMLString:baseURL: and before getting the callback it's not true.)

1
  • The "loading" property does work, but loading is only actually triggered once the UIWebView is on the screen (if you trace webViewDidStartLoad and webViewDidFinishLoad). Tips tend to say "add it to the screen with opacity of 0.01", but that doesn't help for a viewcontroller that's not on-screen yet.... Commented May 29, 2013 at 19:20

2 Answers 2

3

Lots of solutions here I think. A quick one is to load your UIWebView with it's hidden property set to YES. Then set your UIViewController as the UIWebViews delegate and implement:

- (void)webViewDidFinishLoad:(UIWebView *)webView

where you set the property back to NO.

A thing to note is that webViewDidFinishLoad will fire more than once if you have framed/embedded content. So you have to keep track of this. Shouldn't really be a problem if you are loading local content.

3
  • This solution has the user looking at a blank display while the page is drawn offscreen. (For my application I have a background that the transparent page will be rendered in front of, so that's something to look at, but...) It would be better to model other apps that show the control element (button or list item) pressed while the page is rendered, and only then have the fully rendered view animate in.
    – cluesque
    Commented Jun 3, 2009 at 16:20
  • Then have the target/selector of the button drive the rendering, and advance the page when it finishes (webViewDidFinishLoad: as monowerker suggests)
    – Rob Napier
    Commented Jun 4, 2009 at 0:56
  • webViewDidFinishLoad: won't be called until after the UIWebView is moved onto the screen.... Commented May 29, 2013 at 19:21
0

I like monowerker's solution best, but another solution would be to hold onto the already-rendered UIWebView all the time (in some more permanent object than the view controller). I'd only do that if the look of monowerker's solution is too disruptive.

1
  • The UIWebView consumes a lot of memory, and is the first thing thrown overboard when we get a low memory warning. Also one of my use cases is rendering detail of an object selected from a list view (e.g., in Mail you see a list of messages, tap one and read the whole thing) so I can't render until I know which object is to be viewed.
    – cluesque
    Commented Jun 3, 2009 at 16:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.