Question

I'm trying to do some progress indication for when trying to upload and download a file.

Been reading and looks like this is the method that I need to use.

- (void)connection:(NSURLConnection *)connection didSendBodyData:(NSInteger)bytesWritten totalBytesWritten:(NSInteger)totalBytesWritten totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite

I'd like to read more about how that function works (i.e when is it called etc), but can't seem to find the reference documentation. I went to this URL but it does not say anything about that particular function.

https://developer.apple.com/library/mac/#documentation/Foundation/Reference/NSURLConnectionDelegate_Protocol/Reference/Reference.html%23//apple_ref/occ/intf/NSURLConnectionDelegate

Where can I read more about it?

Thank you,
Tee

Was it helpful?

Solution

NSURLConnection had an informal protocol for its delegate. That changed in iOS 5 (I think the same happened on OSX). They deprecated the methods declared in NSURLConnection and moved them into formal protocols NSURLConnectionDelegate and NSURLConnectionDataDelegate.

And now the fun part. They deprecated the methods, they even removed them from the NSURLConnection documentation but they did not document the new formal protocols. Currently, only NSURLConnectionDelegate is documented. NSURLConnectionDataDelegate is not mentioned anywhere.

There are two ways how to find what the method does.

  1. Look into the previous version of NSURLConnection docs (e.g. iOS 4.3). I would give you a link but I couldn't find it online. Maybe you have the library downloaded in your XCode

  2. Press cmd-shift-o in Xcode, type NSURLConnectionDataDelegate and press enter. You have found the header and the methods have a description there:

    connection:didSendBodyData:totalBytesWritten:totalBytesExpectedToWrite: is called during an upload operation to provide progress feedback. Note that the values may change in unexpected ways if the request needs to be retransmitted.

OTHER TIPS

Try AFNetworking It is beocming a popular replacement of ASIHTTPRequest, if you are concerned about support. Also it has a built in support for download progress. (See discussion on this link.) Here is a recent discussion of how to actually use it.

UPDATE

How do I track upload or download progress? (AFNetworking faq)

AFURLConnectionOperation provides the methods setUploadProgressBlock: and setDownloadProgressBlock:. Each method takes a single parameter, which is a block that will be executed periodically during the lifetime of the request. The block has no return type and takes 3 arguments: the number of bytes read or written for this callback, the total number of bytes read or written so far, and the total number of bytes expected to be read or written.

If you wanted to update a progress bar, you could set the respective progress block to set the progress amount to total number bytes read or written divided by the expected number, normalized between 0.0 and 1.0. UI updates based on this value will update asynchronously as the request is being made.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top