0

On my single view I have upload and download option of image and audio file, I successfully implemented the download code with progress bar indicating download progress.

I am having issue in showing uploading progress, currently I am using [NSURLConnection sendAsynchronousRequest: but I also want to show upload progress, this method has no callback block or delegate function regarding data progress. So I tried to use connection:didSendBodyData:totalBytesWritten:totalBytesExpectedToWrite: but how this method will trigger?

For downloading I am doing this to trigger NSURLConnectionDataDelegate methods and getting my job done.

NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

If i use [NSURLConnection connectionWithRequest:someReq delegate:self] for uploading, I will be setting delegate twice in one file, first when uploading method is called and second when download method is called, Is this a correct approach?

Finally how [NSURLConnection sendAsynchronousRequest: is useful, it has no delegate or callbacks regarding data progress, why to use it?

S.J
  • 3,063
  • 3
  • 33
  • 66

1 Answers1

1

Downloading and uploading can be two different moments of the application. It does make sense to set self twice as delegate, if you create twice the object that is responsible for starting the NSURLConnection, otherwise no it doesn't make sense.

The complete method signature is sendAsynchronousRequest:queue:completionHandler: which makes a huge different. In this case you don't need to set a delegate because the response comes in the handler (NSData *).

Rui Peres
  • 25,741
  • 9
  • 87
  • 137
  • please one more thing, it not possible to show upload progress through sendAsynchronousRequest:queue:completionHandler:? am I correct? – S.J Nov 19 '12 at 08:22
  • I think you cant with `sendAsynchronousRequest:queue:completionHandler:`because you only have a `completionHandler` (called when you got what you want). `connection:didSendBodyData:totalBytesWritten:totalBytesExpectedToWrite:`, has been deprecated, so you should not use it. – Rui Peres Nov 19 '12 at 08:32
  • then how to upload asynchronously with uploading progress bar? – S.J Nov 19 '12 at 08:35
  • You can use this example: http://www.developers-life.com/progress-bar-download-file-on-iphone.html – Rui Peres Nov 19 '12 at 08:39
  • connection:didSendBodyData:totalBytesWritten:totalBytesExpectedToWrite: is not deprecated, I referred the apple doc. – S.J Nov 19 '12 at 08:53
  • https://developer.apple.com/library/mac/#documentation/Foundation/Reference/NSURLConnectionDelegate_Protocol/DeprecationAppendix/AppendixADeprecatedAPI.html#//apple_ref/occ/intfm/NSURLConnectionDelegate/connection:didSendBodyData:totalBytesWritten:totalBytesExpectedToWrite: – Rui Peres Nov 19 '12 at 08:54
  • Its NSURLConnectionDelegate reference, but NSURLConnectionDataDelegate now has this method... and here is the apple doc ref https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLConnection.html – S.J Nov 19 '12 at 09:09
  • Use it then... Although I wasn't aware of `NSURLConnectionDataDelegate`, thanks for the reference. – Rui Peres Nov 19 '12 at 09:25
  • you are welcome ... but i am still confused regarding utilising it with both upload and download.. If I am doing this for uploading [NSURLConnection connectionWithRequest:request delegate:self]; other delegate methods are also triggering like "didReceiveResponse:", "didReceiveData:", "connectionDidFinishLoading:" I am using these to handle downloading... – S.J Nov 19 '12 at 09:32