Improving UIWebView initialization time
My company uses UIWebView
to display ads. The problem I'm running into is that initialization UIWebView
seems expensive; profiling with Time Profiler shows [UIWebView alloc] initWithFrame:CGRectMake(0,0,500,500)]
to take 31-40ms. That's enough to cause noticeable frame drops in 60 FPS games.
Is there a workaround for this slow initialization time? My current ideas are to build UIWebView
when the app starts (but before the game starts) and reuse that (potentially creating a reusable pool, like how it works UITableViewCell
) or try to see if it WKWebView
has better performance.
source to share
Here are my findings:
-
WKWebView
does not initialize faster. CreationWKWebView
took about the same amount of time as creationUIWebView
(in 1st test I took 46ms to create twoWKWebView
s. - The very first webview created takes significantly longer than subsequent web views. The first one takes 31-42 ms; subsequent ones take ~ 11ms to create. The good news is that creating the first web view when launching apps, for example, allows future web reviews to be made cheaper by avoiding the 40 second hit while playing.
- Pooling
UIWebView
was a good solution for my use case. By creating webviews when the app starts and then reusing them, I avoid triggering a frame during gameplay.
source to share
There is not much difference in response between UIWebView
and WKWebView
because it WKWebView
was introduced to ensure consistency between iOS and OSX. The main Webkit engine anyway and requires a lot of initialization.
The best solution I have found in recent years fades away WebView
from the 0.1 alpha view where the url is loaded. Be careful not to start at 0.0 or disconnect your webview from the main view hierarchy because your url won't load.
When called didFinishLoading
, you can bring it down to 1.0, thus providing a better user experience. Personally, I don't like the pool UIWebView
because I experienced some memory issues when saving it, especially on iOS 7 devices.
source to share