20

In my iPhone app I am downloading some data from an FTP server. To show the action I am using UIActivityIndicator. If I put UIProgressView there instead of UIActivityIndicator, it will be more appropriate. How do I use UIProgressView while downloading some data? Can anybody give me a tutorial link or example code? Thanks in advance.

Jonathan Grynspan
  • 43,286
  • 8
  • 74
  • 104
Ravi
  • 1,759
  • 5
  • 20
  • 38

5 Answers5

40

first you create IBOutlet in .h file

IBOutlet UIProgressView * threadProgressView;

Then in .m file in viewdidload first set progress to 0.0 and then call makeMyProgressMoving method

    threadProgressView.progress = 0.0;
    [self performSelectorOnMainThread:@selector(makeMyProgressBarMoving) withObject:nil waitUntilDone:NO];

then add below method

- (void)makeMyProgressBarMoving {

        float actual = [threadProgressView progress];
        if (actual < 1) {
            threadProgressView.progress = actual + ((float)recievedData/(float)xpectedTotalSize);
            [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(makeMyProgressBarMoving) userInfo:nil repeats:NO];
        }
        else{



        }

    } 

also give your review for answer. is it useful to you?

Dhaval Rathod
  • 456
  • 4
  • 7
4

It is quite simple. You just need to set appropriate value of property progress of UIProgressView.

In delegate of NSURLConnection you should receive the amount of data you are waiting to download and update the progress during downloading. Progress is represented by a floating-point value between 0.0 and 1.0, inclusive, where 1.0 indicates the completion of the task.

Nekto
  • 17,837
  • 1
  • 55
  • 65
4

You can display progress of progress bar with these line of code

-(void) connection:(NSURLConnection *) connection 
didReceiveData:(NSData *) data {
   if (file)
   { 
       [file seekToEndOfFile];
        progressView.progress = ((float)recievedData / (float) xpectedTotalSize);
   } 
     [file writeData:data];
     recievedData += data.length;
     NSLog(@"Receiving Bytes: %d", recievedData);
}
Bhavin Ramani
  • 3,221
  • 5
  • 30
  • 41
Ketan Shinde
  • 1,847
  • 4
  • 18
  • 38
2

One of the option is AFNetworking. AFURLConnectionOperation also allows you to easily stream uploads and downloads, handle authentication challenges, monitor upload and download progress, and control the caching behavior or requests.

SachinVsSachin
  • 6,401
  • 3
  • 33
  • 39
0

noted: self.progressionBalance.progress = 5.0/10.0;

you must set decimal.

anson
  • 1,436
  • 14
  • 16