0

in my app I am uploading image to server. I am using this method https://stackoverflow.com/a/22301164/1551588. It works fine but I would like add progress bar to this method. It is possible? sendAsynchronousRequest can do? Thank you for response.

Community
  • 1
  • 1
Patrik Dendis
  • 313
  • 5
  • 19

1 Answers1

1

It's seem it's impossible to obtain a progress value in iOS.

But on SO I found a pretty work-around, who is basically cheating, but visually, he does the work.

You fill yourself a progress indicator and at the end you make sure that it is full.

original answer UIWebView with Progress Bar

the code with improvements:

#pragma mark - Progress View

Boolean finish = false;
NSTimer *myTimer;

-(void)startProgressView{
    _progressView.hidden = false;
    _progressView.progress = 0;
    finish = false;
    //0.01667 is roughly 1/60, so it will update at 60 FPS
    myTimer = [NSTimer scheduledTimerWithTimeInterval:0.01667 target:self selector:@selector(timerCallback) userInfo:nil repeats:YES];
}
-(void)stopProgressView {
    finish = true;
    _progressView.hidden = true;
}

-(void)timerCallback {
    if (finish) {
        if (_progressView.progress >= 1) {
            _progressView.hidden = true;
            [myTimer invalidate];
        }
        else {
            _progressView.progress += 0.1;
        }
    }
    else {
        _progressView.progress += 0.00125;
        if (_progressView.progress >= 0.95) {
            _progressView.progress = 0.95;
        }
    }
    //NSLog(@"p %f",_progressView.progress);
}

Here how to use it:

first call (obviously) where you need

[self startProgressView];

and then in the delegate

#pragma mark - NSURLConnection Delegate Methods

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    NSLog(@"Loaded");
    [self stopProgressView];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    NSLog(@"Error %@", [error description]);
    [self stopProgressView];
}

```

Community
  • 1
  • 1
David F.
  • 1,209
  • 12
  • 25