Question

I am trying to migrate to NSURLSession so I can send multiple images to my server and stop when I get the needed response. But my completion block never gets called. Would appreciate if you could tell me if I am doing the migration correctly.

Here is my old code that works fine-

-(void) sendImgWithText:(UIImage*)img
{
    NSURL *requestURL = [NSURL URLWithString:@"Some URL"];

    // create request
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
    [request setHTTPShouldHandleCookies:NO];
    [request setTimeoutInterval:30];
    [request setHTTPMethod:@"POST"];

    NSString *boundary = @"*****";
    NSString *lineEnd = @"\r\n";
    NSString *twoHyphens = @"--";

    // set Content-Type in HTTP header
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
    [request setValue:contentType forHTTPHeaderField: @"Content-Type"];

    // post body
    NSMutableData *body = [NSMutableData data];

    // add params (all params are strings)
    //   UIImage *imgColor = [UIImage imageNamed:@"9.jpg"];

    UIImage * imageToPost = [[UIImage alloc] init];
    UIImageWriteToSavedPhotosAlbum(imageToPost, nil, nil, nil);
    NSData *imageData = UIImageJPEGRepresentation(imageToPost, 1.0);

    if (imageData) {
        [body appendData:[[NSString stringWithFormat:@"%@%@%@", twoHyphens,boundary, lineEnd] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"uploadedfile\"; filename=\"test.jpg\"%@",lineEnd] dataUsingEncoding:NSUTF8StringEncoding]];
        //  [body appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[[NSString stringWithFormat:@"%@",lineEnd] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:imageData];
        [body appendData:[[NSString stringWithFormat:@"%@",lineEnd] dataUsingEncoding:NSUTF8StringEncoding]];
    }

    [body appendData:[[NSString stringWithFormat:@"%@%@%@%@", twoHyphens,boundary, twoHyphens,lineEnd] dataUsingEncoding:NSUTF8StringEncoding]];

    // setting the body of the post to the reqeust
    [request setHTTPBody:body];

    // set the content-length
    NSString *postLength = [NSString stringWithFormat:@"%d", [body length]];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];

    NSLog(@"%@",requestURL);
    NSLog(@"%f,%f",imageToPost.size.height,imageToPost.size.height);

    // set URL
    [request setURL:requestURL];
    NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
}

And here is the version(I got most of it from other stack overflow articles)

- (void) serverRequestWithImage:(UIImage *)img completion:(void (^)(id responseObject, NSError *error))completion
{
    NSURL *requestURL = [NSURL URLWithString:@"Some URL"];

    // create request
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
    [request setHTTPShouldHandleCookies:NO];
    [request setTimeoutInterval:30];
    [request setHTTPMethod:@"POST"];

    NSString *boundary = @"*****";
    NSString *lineEnd = @"\r\n";
    NSString *twoHyphens = @"--";

    // set Content-Type in HTTP header
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
    [request setValue:contentType forHTTPHeaderField: @"Content-Type"];

    // post body
    NSMutableData *body = [NSMutableData data];

    UIImage * imageToPost = [[UIImage alloc] init];
    imageToPost = img;

    NSData *imageData = UIImageJPEGRepresentation(imageToPost, 1.0);
    if (imageData) {
        [body appendData:[[NSString stringWithFormat:@"%@%@%@", twoHyphens,boundary, lineEnd] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"uploadedfile\"; filename=\"test.jpg\"%@",lineEnd] dataUsingEncoding:NSUTF8StringEncoding]];
        //  [body appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[[NSString stringWithFormat:@"%@",lineEnd] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:imageData];
        [body appendData:[[NSString stringWithFormat:@"%@",lineEnd] dataUsingEncoding:NSUTF8StringEncoding]];
    }

    [body appendData:[[NSString stringWithFormat:@"%@%@%@%@", twoHyphens,boundary, twoHyphens,lineEnd] dataUsingEncoding:NSUTF8StringEncoding]];

    // setting the body of the post to the reqeust
    [request setHTTPBody:body];

    // set the content-length
    NSString *postLength = [NSString stringWithFormat:@"%d", [body length]];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];

    NSLog(@"%@",requestURL);
    NSLog(@"%f,%f",imageToPost.size.height,imageToPost.size.height);

    // set URL
    [request setURL:requestURL];

    NSURLSessionTask *task =
    [session
     dataTaskWithRequest:request
     completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)
     {
         // report any network-related errors
         NSLog(@"Got Response 1");

         if (!data) {
             if (completion) {
                 dispatch_async(dispatch_get_main_queue(), ^{
                     completion(nil, error);
                 });
             }
             return;
         }

         // report any errors parsing the JSON
         NSError *parseError = nil;
         _responseData = [NSMutableData dataWithData:data];

         if (_responseData) {
             if (completion) {
                 dispatch_async(dispatch_get_main_queue(), ^{
                     completion(nil, parseError);
                 });
             }
             return;
         }

         // if everything is ok, then just return the JSON object
         if (completion) {
             dispatch_async(dispatch_get_main_queue(), ^{
                 completion(_returnedData, nil);
             });
         }
     }];

    [task resume];
}

And here is my call method from the other class

- (IBAction)GetData:(id)sender
{
    [_imageView setImage:[UIImage imageNamed:@"9.jpg"]];

    msg = [[Message alloc] init];
    [msg initData];
    msg.delegate=self;
    [msg serverRequestWithImage:_imageView.image completion:^(id responseObject, NSError *error)
     {
        if (responseObject) {
            // do what you want with the response object here
            NSLog(@"Got Data");
        } else {
            NSLog(@"%s: serverRequest error: %@", __FUNCTION__, error);
        }
    }];

}
Was it helpful?

Solution

Items:

  • I didn't see the initialization of NSURLSession. That's something you should show in your question. You could also check in your "send image" method that session is non-nil.

  • It looks like you are starting with a file (@"9.jpg"). If so, -[NSURLSession uploadTaskWithRequest:fromFile:completionHandler:] might save you a bunch of trouble.

Unrequested advice, with all that entails: :)

  • In my view, -serverRequestWithImage:completion: is a poor name for that method. It implies that it returns a request object, and does not tell you that it actually sends the request. Something active, like -uploadImage:completionHandler: might be better.

  • Idiomatically, all method names should start lowercase, i.e. -getData:.

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