1

I m trying to upload an Image from iPhone to Server. I m Able to Do so But i need to show an Indicator.My code is

NSData *imageData = UIImageJPEGRepresentation(newImage, 90);

// setting up the URL to post to
NSString *urlString = [NSString stringWithFormat:@"URL",[[formater stringFromDate:now] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
// setting up the request object now
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
  NSString *boundary = [NSString stringWithFormat:@"---------------------------14737809831466499882746641449"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
 NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"image\"; filename=\"ipodfile.jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// setting the body of the post to the reqeust
[request setHTTPBody:body];

Here is the code to fire request.

[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue]
                           completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
                               NSLog(@"Finished with status code: %i", [(NSHTTPURLResponse *)response statusCode]);
 }];

So How can i Calculate the Remaining time so that i can Show an indicator ? Thanks for your time.

  • possible duplicate of [How to make an progress bar for an NSURLConnection when downloading a file?](http://stackoverflow.com/questions/2267950/how-to-make-an-progress-bar-for-an-nsurlconnection-when-downloading-a-file) – lxt Feb 28 '13 at 14:02
  • @lxt thanks for your time But i m not looking for that. I know NSURLconnection Delegate methods But i m doing the Upload inside a Block in separate thread. –  Feb 28 '13 at 14:09

2 Answers2

4

You can use this method of connection delegate:

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

(How should I wrap such long argument names?)
It's not in documentation of NSURLConnectionDataDelegate, but if you check the public header NSURLConnection.h, it's there. So don't worry about using it.

From documentation URL Loading System Programming Guide > Using NSURLConnection:

Estimating Upload Progress

You can estimate the progress of an HTTP POST upload with the connection:didSendBodyData:totalBytesWritten:totalBytesExpectedToWrite: delegate method. Note that this is not an exact measurement of upload progress, because the connection may fail or the connection may encounter an authentication challenge.

Community
  • 1
  • 1
Tricertops
  • 8,492
  • 1
  • 39
  • 41
  • Hi @iMartin Thanks for the Answer. I already Added this to My code But this method Not getting called. I added `NSURLConnectionDelegate` in .h file also. is it due to The Following code ? `[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { NSLog(@"Finished with status code: %i", [(NSHTTPURLResponse *)response statusCode]); }];` –  Feb 28 '13 at 14:33
  • Yes, you will have to use other methods, because you need to set the delegate. For example use `–initWithRequest:delegate:` and then `-start`. Then implement few other delegate methods to check for completion or error. – Tricertops Mar 01 '13 at 10:58
1

I believe you want to show something like a progress bar. I recomend using the MKNetworkKit that provides you many tools for network management, such as progress of an asynchronous connection. Check this link.

CainaSouza
  • 1,417
  • 2
  • 16
  • 31
  • 1
    Hi @CainaSouza Thanks for your response. I don't want to use any third Party Library for this small task. That kit will be usefull for the complex tasks. –  Feb 28 '13 at 14:11