Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use the progress bar in the iphone app

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.

like image 387
Ravi Avatar asked Sep 14 '11 07:09

Ravi


People also ask

What is progress bar in iPhone?

The progress bar shows installation progress. The amount of time depends on the number of files on the device and whether you're erasing, updating, or upgrading your iOS or iPadOS. This process can take as little as a minute if your device has little or no data or if you're erasing the device.

What is an indicator of progress?

Alternatively referred to as a progress bar, a progress indicator is a graphical representation of the current status of a task. It is helpful for estimating the amount of time before a task is completed.


4 Answers

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?

like image 93
Dhaval Rathod Avatar answered Oct 07 '22 15:10

Dhaval Rathod


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.

like image 40
Nekto Avatar answered Oct 07 '22 14:10

Nekto


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);
}
like image 38
Ketan Shinde Avatar answered Oct 07 '22 15:10

Ketan Shinde


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.

like image 44
SachinVsSachin Avatar answered Oct 07 '22 14:10

SachinVsSachin