iPhone - How to preload a local file in a UIWebVie

2019-08-05 19:51发布

Is it possible to preload content of a local file with embedded images for a UIWebView?

This is how I'm loading the data inside the UIWebView:

NSString *urlString = [[NSBundle mainBundle] pathForResource:@"info" ofType:@"html"];
NSString *htmlContent = [NSString stringWithContentsOfFile:urlString encoding:NSUTF8StringEncoding error:nil];
[myWebView loadHTMLString:htmlContent baseURL:nil];

But the UIWebView still needs some time to display the content. Is there any way to put this content into the browser cache on app start?

1条回答
来,给爷笑一个
2楼-- · 2019-08-05 20:24

If you create the browser object on app start and load the HTML string immediately, it should work fine to display the view later.

Another option would be to allocate it when necessary, but not actually show the view until it's done loading (wait for - (void)webViewDidFinishLoad:(UIWebView *)webView to be called on the web view's delegate before showing it).

Also, would it make more sense to call

[myWebView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:urlString]]];

than using the loadHTMLString: method? That way, you don't have to load the HTML string into memory yourself and the web view can load it directly from disk.

查看更多
登录 后发表回答