0

I am writing an IOS file upload application which has multiple NSUrlConnection open at a time (each one for one file upload) and want to implement corresponding progress bar for each one of them.

NSUrlConnection file upload snippet:

NSURLConnection *urlConnection = [[NSURLConnection alloc] initWithRequest:request
                                                                  delegate:self

[urlConnection start];

The delegate method for updating progress bar:

- (void)connection:(NSURLConnection *)connection didSendBodyData:(NSInteger)bytesWritten totalBytesWritten:(NSInteger)totalBytesWritten totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite {
self.progressBar.progress += (float)bytesWritten/(float)totalBytesExpectedToWrite;
NSLog(@"it's working: %lf",self.progressBar.progress);
}

Now in this case if I have a separate progress bar for each file upload, is there a way to know which file upload, that is, which NSURLConnection this delegate corresponds to, so that I can update the correct progress bar accordingly. Is there any property which I can set in NSURLConnection, which I can access in connection variable in the delegate method?

Thanks.

Mohd Prophet
  • 1,511
  • 10
  • 17
Jeet Kumar
  • 195
  • 3
  • 16

1 Answers1

0

What I did is that I built my own class (e.g. MyUploader) and I added the NSURLConnection object inside it, also a reference to the progress bar control that shall be updated by this NSURLConnection object.

@interface MyUploader

@property (strong, nonatomic) NSURLConnection* connection;
@Property (weak, nonatomic) UIProgressBar bar;

- (void)startUploadingFile:(NSString*)filePath withBarToUpdate:(UIProgressBar*)progressBar;

@end 

In your view controller you can pass the bar object and the file path in the startUploadingFile:withBarToUpdate: and it could store the bar object and starts the connection.

Also this class shall implement the NSURLConnectionDelegate to update the progress bar as you did in your code

Boda
  • 1,484
  • 1
  • 14
  • 28