How to use UIProgressView while loading of a UIWebView?

不想你离开。 提交于 2019-11-26 20:10:27

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.

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

Two caveats:

  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.

  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.

You can try to use this subclass of UIWebView which uses the 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

Using NSURLConnection you are fetching the same data twice,waste of time because it can slow down the user interaction it loads the data twice ,consumes internet data.It is better to make a uiprogress based on timer and when the webview successfuly loaded the webpage it will show the uiprogress loading.in that case you can show a dynamic uiprogress everytime the webpage is loaded.. dont forget to create a uiprogress view and named it myProgressview and set it in file owner's.

HERE IS THE CODE HOPE IT HELPS

@synthesize myProgressView;
- (void)updateProgress:(NSTimer *)sender
{      //if the progress view is = 100% the progress stop
    if(myProgressView.progress==1.0)
    {
        [timer invalidate]; 
    }
   else
         //if the progress view is< 100% the progress increases
       myProgressView.progress+=0.5;
}
- (void)viewDidLoad
{ //this is the code used in order to load the site
    [super viewDidLoad];
    NSString *urlAddress = @"http://www.playbuzz.org/";
    myWebview.delegate = self;
   [myWebview loadRequest:[NSURLRequest requestWithURL:[NSURL     URLWithString:urlAddress]]];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{    ///timer for the progress view

 timer=[[NSTimer scheduledTimerWithTimeInterval:0.1
                          target:self
                          selector:@selector(updateProgress:)
                          userInfo:myProgressView
                          repeats:YES]retain];

}

- (void)dealloc {
    [myProgressView release];
    [super dealloc];
}
@end

this code and idea really helps me in solving my problem.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!