1. Overview
1.1 Header
2. Working with NSURLConnection
2.1 Preflighting a Connection Request
2.2 Connection URL Information
2.3 Loading Data Synchronously
2.4 Loading Data Asynchronously
2.5 Stopping a Connection
3. Working with RESTful Web Services
4. Working with JSON
4.1 What is JSON?
4.2 JSON’s basic types are:
4.3 NSJSONSerialization
5. Exercise 6
5. Nikmesoft Ltd
Overview
Header
Field names
• http://en.wikipedia.org/wiki/List_of_HTTP_header_fields
• Additional field names and permissible values may be defined by each
application.
Size limits
• There are no limits to the size of each header field name or value or to the
number of headers in the standard itself. However, most servers, clients, and
proxy software impose some limits for practical and security reasons. For
example, the Apache 2.3 server by default limits each header size to 8190
bytes, and there can be at most 100 headers in a single request
5
9. Nikmesoft Ltd
NSURLConnection
An NSURLConnection object provides support to perform
the loading of a URL request. The interface for
NSURLConnection is sparse, providing only the controls to
start and cancel asynchronous loads of a URL request.
NSURLConnection’s delegate methods—defined by the
NSURLConnectionDelegate protocol—allow an object to
receive informational callbacks about the asynchronous load
of a URL request.
9
20. Nikmesoft Ltd
Working with RESTful Web Services
20
URL
HEADER
BODY
POST
http://nikmesoft.com/steven/vietfooding/users/login.json
api_key = a30e38ce87e30dd2a6f7e858ce68dcc3
email = test02@gmail.com
password = 1
22. Nikmesoft Ltd
Working with RESTful Web Services
22
URL
HEADER
GET
http://nikmesoft.com/steven/vietfooding/users/?id=1/
api_key = a30e38ce87e30dd2a6f7e858ce68dcc3
24. Nikmesoft Ltd
Working with JSON
What is JSON?
JSON or JavaScript Object Notation, is a text-based open
standard designed for human-readable data interchange. It is
derived from the JavaScript scripting language for representing
simple data structures and associative arrays, called objects.
Despite its relationship to JavaScript, it is language-independent,
with parsers available for many languages.
The JSON format is often used for serializing and transmitting
structured data over a network connection. It is used primarily to
transmit data between a server and web application, serving as an
alternative to XML.
24
25. Nikmesoft Ltd
Working with JSON
JSON’s basic types are:
Number
String (double-quoted Unicode, with backslash escaping)
Boolean (true or false)
25
26. Nikmesoft Ltd
Working with JSON
JSON’s basic types are:
Array (an ordered sequence of values, comma-separated and
enclosed in square brackets; the values do not need to be of the
same type)
Object (an unordered collection of key:value pairs with the ':'
character separating the key and the value, comma-separated and
enclosed in curly braces; the keys must be strings and should be
distinct from each other)
null (empty)
26
28. Nikmesoft Ltd
Working with JSON
NSJSONSerialization
You use the NSJSONSerialization class to convert JSON to
Foundation objects and convert Foundation objects to JSON.
An object that may be converted to JSON must have the following
properties:
• The top level object is an NSArray or NSDictionary.
• All objects are instances of NSString, NSNumber, NSArray, NSDictionary, or
NSNull.
• All dictionary keys are instances of NSString.
• Numbers are not NaN or infinity.
28
29. Nikmesoft Ltd
Working with JSON
NSJSONSerialization
Other rules may apply. Calling isValidJSONObject: or attempting a
conversion are the definitive ways to tell if a given object can be
converted to JSON data.
Creating a JSON Object
• + JSONObjectWithData:options:error:
• + JSONObjectWithStream:options:error:
Creating JSON Data
• + dataWithJSONObject:options:error:
• + writeJSONObject:toStream:options:error:
• + isValidJSONObject:
29
30. Nikmesoft Ltd
Working with JSON
NSJSONSerialization
JSONObjectWithData:options:error:
• NSJSONReadingMutableContainers
– Specifies that arrays and dictionaries are created as mutable objects.
• NSJSONReadingMutableLeaves
– Specifies that leaf strings in the JSON object graph are created as instances of NSMutableString.
• NSJSONReadingAllowFragments
– Specifies that the parser should allow top-level objects that are not an instance of NSArray or
NSDictionary.
30