如何在加载UIWebView时使用UIProgressView? [英] How to use UIProgressView while loading of a UIWebView?

查看:134
本文介绍了如何在加载UIWebView时使用UIProgressView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应用程序,我在UIWebView中加载urlrequest并且它成功发生。

i am developing an application where i am loading a urlrequest in the UIWebView and it happened successfully.

但是现在我想在加载时显示UIProgressView正在进行中(从0.0开始到1.0),随着加载的进度动态改变。

But now i am trying to display a UIProgressView when the loading is in progress( starting from 0.0 to 1.0), which is changed dynamically with the progress of loading.

我该怎么做?

推荐答案

UIWebView在正常模式下不会提供任何进度信息。您需要做的是首先使用NSURLConnection异步获取数据。
当NSURLConnection委托方法 connection:didReceiveResponse 时,你将从 expectedContentLength 并将其用作最大值。然后,在委托方法 connection:didReceiveData 中,您将使用NSData实例的 length 属性来告诉你你走了多远,所以你的进度分数将是 length / maxLength ,归一化到0.0到1.0之间。

UIWebView doesn't give you any progress information in the normal mode. What you need to do is first fetch your data asynchronously using an NSURLConnection. When the NSURLConnection delegate method connection:didReceiveResponse, you're going to take the number you get from expectedContentLength and use that as your max value. Then, inside the delegate method connection:didReceiveData, you're going to use the length property of the NSData instance to tell you how far along you are, so your progress fraction will be length / maxLength , normalized to between 0.0 and 1.0.

最后,您将使用数据而不是URL(在连接中:didFinishLoading 委托方法)初始化webview。

Finally, you're going to init the webview with data instead of a URL (in your connection:didFinishLoading delegate method).

两个警告:


  1. 有可能 expectedContentLength NSURLResponse的属性将是 -1 NSURLReponseUnknownLength 常量)。在这种情况下,我建议抛出一个标准的UIActivityIndi​​cator,你在连接中关闭:didFinishLoading

  1. It is possible that the expectedContentLength property of the NSURLResponse is going to be -1 (NSURLReponseUnknownLength constant). In that case, I would suggest throwing up a standard UIActivityIndicator that you shut off inside connection:didFinishLoading.

确保每次从NSURLConnection委托方法之一操作可见控件时,都可以通过调用 performSelectorOnMainThread:来执行此操作 - 否则您将开始获得可怕的EXC_BAD_ACCESS错误。

Make sure that any time you manipulate a visible control from one of the NSURLConnection delegate methods, you do so by calling performSelectorOnMainThread: - otherwise you'll start getting the dreaded EXC_BAD_ACCESS errors.

使用这种技术,当你知道你应该知道多少数据时,你可以显示一个进度条当你不知道时,得到一个微调器。

Using this technique, you can show a progress bar when you know how much data you're supposed to get, and a spinner when you don't know.

这篇关于如何在加载UIWebView时使用UIProgressView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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