views:

991

answers:

2

Hi,

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

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.

How can i do that?

A: 

Stack Overflow: How to integrate NSURLConnection with UIProgressView?

Alex Reynolds
Unfortunately, I don't think that UIWebView gives you any progress information, like you would with an NSURLConnnection.
Brad Larson
If you use NSURLConnection to get the data to put into a web view, you could observe download progress that way.
Alex Reynolds
+2  A: 

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 c*onnection: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 and 1.

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

One caveat: 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.

Caveat #2: 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.

Kevin Hoffman