0

I want the progressView working while my URL is loading. What should I do for that?

here's my .m code:

-(void)openURL{
UIWebView *webView = [[UIWebView alloc] init];
[webView setFrame:CGRectMake(0, 0, 320, 460)];
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://google.com"]]];
[[self view] addSubview:webView];
}

- (IBAction)goto:(id)sender {


[self openURL];
}

2 Answers2

1

Implement UIWebViewDelegate, specifically:

-(void)openURL{
    UIWebView *webView = [[UIWebView alloc] init];
    webView.delegate = self;
    // ... rest of method as above ...
}

- webViewDidStartLoad:(UIWebView *)webView {
    // Start progress.
}

- webViewDidFinishLoad:(UIWebView *)webView {
    // Stop progress.
}
i_am_jorf
  • 53,608
  • 15
  • 131
  • 222
  • Whilst technically true this doesn't explain how a progress bar can be used. Those delegate callbacks would only signal a 0 to 100 call – Daniel Galasko Nov 18 '14 at 20:43
  • 1
    There is no way to get incremental progress info from UIWebView, you can only show an indefinite spinner. If the question is to show incremental progress, then it's a dupe of [this question](http://stackoverflow.com/questions/1900151/how-to-use-uiprogressview-while-loading-of-a-uiwebview) and many others. – i_am_jorf Nov 18 '14 at 20:45
  • The OP probably needs to be a bit clearer then, one upvote to you sir. – Daniel Galasko Nov 18 '14 at 20:54
  • Eh, your criticism was fair. They probably did want progressive. – i_am_jorf Nov 18 '14 at 21:03
0

You could do something like this

[progressView startAnimating];

dispatch_async(dispatch_get_main_queue(), ^{
    [webView loadRequest:yourRequest];

    [progressView stopAnimating];
    [progressView removeFromSuperView];
}

This will start the progress view before the operation begins, and will stop and remove it when it finishes.

Eilon
  • 2,698
  • 3
  • 16
  • 32