iPhone File Upload Progress Bar
July 3rd, 2009 by sSo I’ve been working on an iPhone app that uploads a file to a website through a POST request. At first I just used a standard Activity Indicator during the upload, but every time I would test out the app out around town, it made for a terrible user experience because uploading an image can take a while.
I’d tried a few times to get a nice Progress Bar showing how many bytes have been uploaded, but gave up because I could never get it to work. Then today I noticed the didSendBodyData delegate method for NSURLConnection. I don’t know how I missed this before, but I feel like an idiot.
NSURLConnection Class Reference
connection:didSendBodyData:totalBytesWritten:totalBytesExpectedToWrite:
Sent as the body (message data) of a request is transmitted (such as in an http POST request).- (void)connection:(NSURLConnection *)connection didSendBodyData:(NSInteger)bytesWritten totalBytesWritten:(NSINteger)totalBytesWritten totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite
Parametersconnection
The connection sending the message.
bytesWritten
The number of bytes written in the latest write.
totalBytesWritten
The total number of bytes written for this connection.
totalBytesExpectedToWrite
The number of bytes the connection expects to write.Discussion
This method provides an estimate of the progress of a URL upload.
The value of totalBytesExpectedToWrite may change during the upload if the request needs to be retransmitted due to a lost connection or an authentication challenge from the server.
Posted in iphone |
1 Comment »
August 11th, 2009 at 1:10 am
Thanks for this hint. The callback is working for me, But it seem, that this callback returns the number of bytes written to a cache or something like that. I am sending about 105000 bytes over edge, but the callback only gets called 4 times where byteswritten is always 32768.