0

I want to display a progress bar, when user will upload any image to the server. my image is uploading successfully to the server using php url. But for this i am not using any NSUrlConnection class. Actually i am uploading multiple of images from different different view controller classes, so i have added a method in my Util class for common uploading image, where i am passing image data & image ID. So how can i implement NSUrlConnection delegates in my below case :

Here is my Util Class Method :

+(void)uploadImageOnServer:(NSData *)imageData forId:(NSString *)imageId
{
     NSString *urlString = @"http://url...............";

    // setting up the request object now
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;

    [request setURL:[NSURL URLWithString:urlString]];

    [request setHTTPMethod:@"POST"];

    NSString *boundary = @"---------------------------147378098314876653456641449";

    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];

    [request addValue:contentType forHTTPHeaderField: @"Content-Type"];

    /*
     now lets create the body of the post
     */
    NSMutableData *body = [NSMutableData data];

    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

    NSString *stringData = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"Documents\"; filename=\%@.jpg\r\n",imageId];

    [body appendData: [stringData dataUsingEncoding:NSUTF8StringEncoding]];

    [body appendData:[@"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];

    // now lets make the connection to the web
    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

    NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
}

From My ViewController class, i am calling above method for uploading an image to server as:

[Util uploadImageOnServer:positionData forId:positionImageId];

It is taking around 4-5 seconds for uploading one image to the server. I want to add progress view like this attached screenshot. Can you pls guide me how can i integrate common NSUrlConnection delegates in my Util Class for displaying to the user that how much data has been uploaded ?

Thanks.

enter image description here

Raghu Kaligipula
  • 404
  • 2
  • 5
  • 14

1 Answers1

1

You need to use the asynchronous connection and the connection:didSendBodyData:totalBytesWritten:totalBytesExpectedToWrite: delegate method.

You should also consider using AFNetworking and the progress callbacks that it offers.

Wain
  • 118,658
  • 15
  • 128
  • 151
  • Thanks@WainMy problem is everytime i am invoking Util method for uploading images, so how can i call didSendBodyData NSUrlConnection delegate method. Can you pls guide me in my code ? – Raghu Kaligipula Jan 20 '14 at 10:44
  • The connection would call the method, you need to provide some way to use the information, like the `Util` also taking a block parameter which takes the progress as a parameter (which is basically the interface AFNetworking presents). – Wain Jan 20 '14 at 10:51
  • how can i get NSURLRequest with URLWithString for this Util method? then only i can get uploading percentage in didReceiveData delegate method.My Util mehod is returning only server success or fail message.. – Raghu Kaligipula Jan 20 '14 at 10:59
  • You need to rethink your whole approach - you can't use a synchronous approach so you need to make the Util class maintain a list of connections and associated callbacks for progress and success. -- AFNetworking -- is a better way to go than doing it yourself. – Wain Jan 20 '14 at 11:02