UIWebView lower loading time with a more efficient

2019-07-17 04:44发布

I'm downloading multiply htmls and save them locally as strings in an NSArray. Then I'm using 3 UIWebViews to load the content. The user always sees one UIWebView and 2 more UIWebViews are loaded in the background using:

[_firstWebView loadHTMLString:nextHtml.body baseURL:nil];

When the user moves around between UIWebViews I can still see slow loading times of 1-2 seconds if he moves 2 UIWebViews at a time. I thought about going to 5 or even 7 UIWebViews but I'm afraid it will impact the memory usage heavily.

Another thought I had was saving the content locally as NSData and then load it with

[web loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:filePath]]]; 

But I'm not sure that will gain me any improvement or will it?

Another thought was that something in my app (animation on the screen) is slowing down the loading time so maybe I should loadHTMLString:nextHtml on a different thread?

Thanks

1条回答
倾城 Initia
2楼-- · 2019-07-17 05:21

Recently I did some research about loading content to UIWebView in the background. I've found that even successful loadRequest has no effect while webview is invisible and not added as subview somewhere.

But if I make some access to view before loading, it starts to show content loaded in the background.

So I hide the view first, then load content in it and before showing it to user make the view visible again:

In loading method:

// make some access to view to reflect content changes
self.webView.hidden = YES;

[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:someurl]]];

When user switches to the webview:

// make view visible again
self.webView.hidden = NO;

This approach worked for me. Hope it also point you in right direction.

查看更多
登录 后发表回答