Sunday, June 22, 2014

Ever since iOS5, the iOS supports the JSON parsing API. The NSJSONSerialization Class could convert JSON to Foundation objects, and convert Foundation objects to JSON. The Foundation objects supported in the API are NSDictionary, NSArray, NSString, NSNumber. (Checkout the Document for detail.)
So, making a HTTP Request on iOS, it requires a NSURLRequest(HTTP Request) configured appropriately, and the NSData to be sent to the server.

Here, the code
1st, Create a NSURLSession, which will generate a URLTask based on the URLRequest created later.
2nd, Create a NSURLRequest. Since we need to configure the request, then we make it mutable here. The exact URL is the string url, to which is the Restful API of the server.
3rd, Configure the Request with Method, Body(Data) and HTTPHeader.
4th, Create a DataTask which accomplishes the actual request operation. It has a Callback(Block) in it, which will be fired once the request accomplished, data will be received if the server returns any data, like confirmation data, etc.
5th, Fire the DataTask.