1

I've followed some tutorials, but I'm stuck on doing Post requests. I Just want to send 3 parameters, to a URL and hadle with the response. And it has to be asynchronous, because it will give me some images, that i want to but one by one on the view.

Can you help me guys?

88fsantos
  • 393
  • 1
  • 7
  • 22

2 Answers2

2

This is well covered here.

But the way I do it I find to be simpler, as I'll show you. Still there are many questions here on SO and other places that provide this knowledge.

First we set up our request with our parameters:

- (NSData *)executePostCall {
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@", YOUR_URL]];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

    NSString *requestFields = [NSString stringWithString:@""];
    requestFields = [requestFields stringByAppendingFormat:@"parameter1=%@&", parameter1];
    requestFields = [requestFields stringByAppendingFormat:@"parameter2=%@&", parameter2];
    requestFields = [requestFields stringByAppendingFormat:@"parameter3=%@", parameter3];

    requestFields = [requestFields stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSData *requestData = [requestFields dataUsingEncoding:NSUTF8StringEncoding];
    request.HTTPBody = requestData;
    request.HTTPMethod = @"POST";

    NSHTTPURLResponse *response = nil;
    NSError *error = nil;
    NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; 
    if (error == nil && response.statusCode == 200) {
      NSLog(@"%i", response.statusCode);
    } else {
      //Error handling
    }

    return responseData;
}

This has to be wrapped up in a block since we can't execute this on the main thread because it will lock up our application and that is frowned upon, so we do the following to wrap this request up, I'll leave the rest of the details up to you:

dispatch_queue_t downloadQueue = dispatch_queue_create("downloader", NULL);

dispatch_async(downloadQueue, ^{
  NSData *result = [self executePostCall];
  dispatch_async(dispatch_get_main_queue(), ^{
    // Handle your resulting data
  });
});
dispatch_release(downloadQueue);
Community
  • 1
  • 1
8vius
  • 5,786
  • 14
  • 74
  • 136
  • Tks a lot man! I just have one problem. I get no response :S I've tested with javascript and the service gives me the proper response. But with this method, it returns a empty array :S – 88fsantos Jul 04 '12 at 17:25
  • -1: creating a synchronous request and then use it in a new thread? Why don't you just create an asynchronous request? – Sulthan Jul 04 '12 at 20:48
  • It's simply the solution I implemented for the current application I'm developing, I have a class with several class methods that define the different requests I use which I call in blocks in my controllers. I found it simpler and more straight forward than using asynchronous requests, and also seems I'm more in control. – 8vius Jul 04 '12 at 21:00
  • 2
    If you have good resources on how to do asynchronous requests, please post them, I'm open to suggestions and improvements. – 8vius Jul 04 '12 at 21:02
  • `NSURLConnection` documentation is the best resource :) Come on, it's really simple. – Sulthan Jul 05 '12 at 09:44
  • I'm just a front end guy, from multimedia, trying to enter in Apple world. That's why I think that Apple doc. is to difficult to follow. Can you suggest any good book for me? – 88fsantos Jul 05 '12 at 09:55
  • The book isn't gonna be that much simpler. I'd suggest to go on iTunes and look for Stanford's iPhone and iPad development course on iTunes U. It's very simple, straight forward and you will learn a great deal. – 8vius Jul 05 '12 at 18:20
  • Interesting answer @8vius - Has helped me a lot today. Finally managed to get POST request going. But lets say you wanted to attach a picture... how does that get added to the string? – Supertecnoboff Apr 14 '14 at 11:56
  • @Supertecnoboff seems this might help you http://stackoverflow.com/questions/16434537/post-image-to-server-in-iphone – 8vius Apr 18 '14 at 16:24
0

Use NSURLRequest. You can download files in the background and show them once you receive the delegate notification: Downloading to a Predetermined Destination

DrummerB
  • 39,814
  • 12
  • 105
  • 142