9

I am using UIWebView to load a web page.

There are 3 questions:

1.It it possible to track the percentage progress when UIWebView is loading the page?

2.I noticed that when Safari loading a web page, the URL textfield displays a blue background progress indicator to tell user the percentage of loading a web page. What is the technology for this?

3.I know there is property scalesPageToFit

scalesPageToFit A Boolean value determining whether the webpage scales to fit the view and the user can change the scale.

I try to set it to YES, but it looks like that it is not in public API and my app stopped with black screen, I am not sure what is wrong?

luk2302
  • 55,258
  • 23
  • 97
  • 137
arachide
  • 8,006
  • 18
  • 71
  • 134

5 Answers5

21

To answer #1)

Instead of using a UIWebView, you can pull the webpage down as an NSData object using an NSURLConnection. When you get the initial response from your request from

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response

the webserver should return a value of "expected content size" (which should be included in the response). Then you will keep getting the following method called each time you receive data:

 - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data

Keep appending the data to an existing NSMutableData object. Then you can check the size of your current data object (NSMutableData.length) against the expected response size.

percentage = (myData.length*100)/theResponse.expectedContentSize;

Then you can update a progress bar with that percentage! When

- (void)connectionDidFinishLoading:(NSURLConnection *)connection

runs, use your data to call

[myWebView loadData:myData MIMEType:myMimeType textEncodingName:myEncoding baseURL:baseURL];

and it will load everything you pulled down into your web view.

Ben G
  • 3,965
  • 3
  • 30
  • 34
Pyro2927
  • 1,112
  • 9
  • 18
  • 4
    Note that this method doesn't tell you when the "whole" page is loaded, only that the first "index.html" file has. But this "index.html" page may very well contain images, scripts, and other resources that need to be loaded before the web page can be truely considered "loaded". But getting it really right would mean parsing the HTML yourself. – Ben G Jul 26 '11 at 13:02
  • Actually, the whole matter has already been discussed here : http://stackoverflow.com/questions/1900151/how-to-use-uiprogressview-while-loading-of-a-uiwebview , along with a private-API solution. – Ben G Jul 26 '11 at 13:19
4

Re #3:

You can try specifying scalePagesToFit as a YES in the viewDidLoad event of the UIView that contains your webview, e.g:

- (void)viewDidLoad {
    self.webView.scalesPageToFit = YES;
    //other code here...
}

For cases where this doesn't work, refer to the following StackOverflow question: UIWebView does not scale content to fit where the asker (and subsequently, answerer) gave this solution.

Apparently you can use javascript to resize your page to fit the browser:

NSString *jsCommand = [NSString stringWithFormat:@"document.body.style.zoom = 1.5;"];
[webLookupView stringByEvaluatingJavaScriptFromString:jsCommand];
Community
  • 1
  • 1
Jon Limjap
  • 94,284
  • 15
  • 101
  • 152
0

To answer question #1, there is a solution that is App Store safe, and uses a similar method as Webkit to track progress.

https://github.com/otium/OTMWebView

Otium
  • 1,098
  • 8
  • 20
0

You can try to use this subclass of UIWebView which uses private UIWebView methods - therefore, this solution is not 100% AppStore safe (though some apps do almost 100% use it: Facebook, Google app, ...).

https://github.com/petr-inmite/imtwebview

joshis
  • 347
  • 4
  • 9
0

Re #2: I believe Safari uses a private API call on UITextField (so you can't do it if you want to submit to the app store), but you should be able to implement this yourself by putting a textfield with no border over the top of a progress bar.

Jarsen
  • 7,432
  • 6
  • 27
  • 26