Sunday, January 25, 2015

iOS Tutorial - Part 27 - HttpRequest POST, GET (NSURLConnection) II

HttpRequest POST, GET (NSURLConnection) II


Video Description 
This is the second part of NSURLConnection tutorial. If you have not read or watched the first part, click here to watch it first. In this part we will learn how to create a GET and POST request.

How to create GET request

The following method will do the GET request
- (void)httpGetRequest
{
    NSString *str = @"http://content.guardianapis.com/search?api-key=test";
    str = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *url = [NSURL URLWithString:str];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:GET];
    self.restApi.delegate = self;
    [self.restApi httpRequest:request];
}
In the first line we add the url address that we want to send the GET request. In the second line we convert the special characters of the url in first line. In the third like we convert the str into URL type that Objective-C can read it as an URL. In the fourth line we create our actual request with the URL we created. In the fifth line we specify the Http type (Here it's GET). The sixth line we are setting restApi delegate equal to self to implement RestApi class. In the last line we call RestAPI class to send our request (RestApi is created in previous session).

How to create POST request

- (void)httpPostRequest
{
    NSString *postBody = @"api-key=test";
    NSString *str = @"http://content.guardianapis.com/search";
    str = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *url = [NSURL URLWithString:str];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:POST];
    [request setHTTPBody:[postBody dataUsingEncoding:NSUTF8StringEncoding]];
    self.restApi.delegate = self;
    [self.restApi httpRequest:request];
}
It's exactly like sending GET request, the only difference is that it has 2 more lines. The first line and the seventh line. In the first line specifies the post body, which may includes post parameters. In the seventh line we attach the post body to the request.

What to do next

Click here for the third part of Http Request

7 comments:

  1. Thank you a lot for your tutorial, it was very helpfull.

    ReplyDelete
  2. these tutorials are too good, Why don't you make new, latest tutorials?

    ReplyDelete
    Replies
    1. I'm very busy with my current job but I'm managing to create new tutorials. I just uploaded one. It's very short but it is necessary for the ones that want to start programming from swift. So you may already know the things that I mention in this tutorial but I promise in second tutorial you will see new content.

      Delete
  3. Is the source code for this tutorial available anywhere? I've been studying the material, implementing it line by line as you explain it in the videos. It all looks right to me. But in the end, my delegate's implementation of getReceivedData is not being invoked. So I must have missed something somewhere. It would be very helpful to compare your working source line by line with my own work to see what i've overlooked.

    ReplyDelete
    Replies
    1. Thanks Charles for following tutorials. I had to put a note that the final project is in the next session. Here is the link of the next session:
      http://huxtek.blogspot.com/2015/02/ios-tutorial-part-28.html
      You can download the final project there.

      Delete
    2. Thank you so much for this work. My current project requires a similar solution so I thought I'd use this as a basis. But damn, I have been struggling! The problem has been in RestAPI.m in connectionDidFinishLoading. the line...

      [self.delegate getReceivedData:theReceivedData sender:self];

      does not call into the delegate's implementation. Also, when I right click on delegate in the 'self.delegate expression at the start of this line of code and choose "Jump to Definition", Xcode gives me a huge list of possible implementations, starting with NSFileManager and going on to [NSAnimation delegate. See this screenshot...

      i.imgur.com/6OeE6Hz.png

      So, I think somehow that the object isn't resolving or is nil or something. I even changed RestAPI.m to not use "lazy" instantiation but to actually implement an init that allocates the two instance variables. It didn't help either.

      Anyway, I will take a look at the project source in session 28. Hopefully it will resolve this problem that I've been fighting for half a day now!

      Delete