预加载webView不起作用-尝试减少加载时间 [英] Preloading webView doesn't work - trying to reduce loading time

查看:504
本文介绍了预加载webView不起作用-尝试减少加载时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带webView的viewController,在其中显示本地html文件.

I have a viewController with a webView where I am displaying a local html file.

问题在于加载之前需要花费几秒钟的时间,因此我试图避免这种等待时间.

The problem is that it takes some seconds before loading therefore I am trying to avoid this waiting time.

到目前为止,我认为最好的方法是: 进入主页后,我便开始将文件加载到webView中,这样,当我使用webView进入infoViewController时,该文件便已被加载.

The best way I thought so far is the following: as soon as I get into the main page I will start to load the file into the webView, in this way when I will get into the infoViewController with the webView it will be already loaded.

这是代码:

//mainViewController.m:

tempInfo = [[InfoViewController alloc]initWithNibName:@"InfoViewController" bundle:nil];

[tempInfo.webView123 loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:@"About" ofType:@"htm"]isDirectory:NO]]];
tempInfo.test =@"test";

如您所见,我还添加了一个测试变量,当我进入infoViewController时,未加载webView,但测试变量更改了值.这意味着它正在传递测试变量的值,但未将html文件加载到webView中.

As you can see I also added a test variable, when I get into the infoViewController the webView is not loaded but the test variable changed value. This means that it is passing the value for the test variable but it is not loading the html file into the webView.

有什么主意吗?

推荐答案

我假设您正在使用iOS.

Im assuming you are using iOS.

一个WebView并不便宜,并且作为SDK的一部分提供的WebView并不比本地Safari窗口好.

A WebView is not cheap and the one that is supplied as part of SDK is less good than native Safari windows.

修改变量的原因是loadRequest是异步的.

The reason that your variable is modified is that loadRequest is async.

您实际上是在要求Webview启动&从冷加载页面.该页面将在将来的某个时刻完成加载.您可以通过实施适当的委托方法来找出时间.

You are effectively asking the Webview to startup & load your page from cold. The page completes load at a point in the future. You can find out when by implementing the appropriate delegate method.

一些您可能想尝试的事情.

Some things you might want to try.

如果视图控制器是单发(使用和丢弃),请尝试将请求加载到initWithNibName中,并将Webview放在层次结构中的viewDidLoadviewWillAppear

If your view controller is single-shot ( use and discard ) try loading the request in initWithNibName and placing the webview in the hierarchy in viewDidLoad or viewWillAppear

通过加载非常简单的"Hello World"文本文件来查找您的页面.如果简单文件花费的时间少得多,那么您的页面可能需要一些优化.

Find out if its your page by loading a very simple "Hello World" text file. If the simple file takes a lot less time then maybe your page needs some optimization.

使用页面加载委托方法

– webViewDidStartLoad:

– webViewDidFinishLoad:

– webView:didFailLoadWithError:

找出关于未加载"页面的假设是否正确.

To work out if your assumptions with respect to the page "not loading" are correct.

祝你好运.

这篇关于预加载webView不起作用-尝试减少加载时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆