iOS development Crash course in how to build an native application for iPhone.
i will be start from beginning till publishing on Apple Store step by step.
session 7 is about how to get data for network using NSURLConnection and
Data Exchange format"JSON"
this session # 7
4. network requests steps
NSURLConnection —> responsible for making call
NSURLConnectionDelegate —> protocol to be conformed
to handle call back methods
1.Have our class conform to the
NSURLConnectionDelegate protocol and declare
a var to store the response data
2.Implement the NSURLConnectionDelegate
protocol methods
3.Create an instance of NSURLRequest and
NSURLConnection to kick off the request
5. network requests
We need to make the ViewController class
implement the NSURLConnectionDelegate.
This means that our class is capable of handling
the delegate callbacks used by a NSURLConnection.
Lastly, we need to add a private NSMutableData
field named _receivedData.
7. NSURLConnectionDelegate
The first thing we need to do is create a NSURLRequest.
This object is initialized with the URL we want to connect to.
Additionally we specify a cache policy and a timeout interval.
After that is done we create a NSURLConnection with the
request as a parameter as well as telling it that it’s delegate, is
the ViewController instance we’re in (so self).
Creating this request also starts it going.
Provided it was created successfully, we will instantiate the
_receivedData object we added to the header.
9. NSURLConnectionDelegate
Specifically the methods we need to implement are
connection:didReceiveResponse, connection:didReceiveData,
connection:didFailWithError, and
connectionDidFinishLoading.
didReceiveResponse is called when a the response first starts to
come in.
didRecieveData may be called once, or may be called multiple
times depending on how much data is returned and how it is
split up.
didFailWithError is called if there was an issue with the request.
connectionDidFinishLoading is called when the response has
fully come over.
10. Exchanging Data
When exchanging data between a Application
and a server, the data can only be text.
JSON is text.
Mapping any JSON received from the server into
Object variables.
11. JSON
•JSON: JavaScript Object Notation.
•JSON is a syntax for storing and
exchanging data.
•JSON is text, written with JavaScript
object notation.
12. What is JSON?
JSON is a lightweight data-interchange format
JSON is "self-describing" and easy to understand
JSON is language independent *
Why we use JSON?
Since the JSON format is text only, it can easily
be sent to and from a server, and used as a data
format by any programming language.
13. JSON Syntax Rules
Data is in name/value pairs
Example : "name":"John"
Data is separated by commas
Curly braces hold objects
{ "name":"John" }
Square brackets hold arrays
14. JSON vs XML
JSON Example
{"employees":[
{ "firstName":"John", "lastName":"Doe" },
{ "firstName":"Anna", "lastName":"Smith" },
{ "firstName":"Peter", "lastName":"Jones" }
]}
15. JSON vs XML
XML Example
<employees>
<employee>
<firstName>John</firstName> <lastName>Doe</lastName>
</employee>
<employee>
<firstName>Anna</firstName> <lastName>Smith</
lastName>
</employee>
</employees>
16. JSON is Like XML
Because
Both JSON and XML are "self
describing" (human readable)
Both JSON and XML are hierarchical
(values within values)
Both JSON and XML can be parsed and
used by lots of programming languages
17. JSON is Unlike XML
Because
JSON doesn't use end tag
JSON is shorter
JSON is quicker to read and write
JSON can use arrays
JSON is faster and easier than XML
18. JSONSerialization
You use the NSJSONSerialization class
to convert JSON to Foundation objects
and convert Foundation objects to
JSON.
19. JSONSerialization
Overview
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.
20. id json = [NSJSONSerialization
JSONObjectWithData:dataObject
options:0 error:nil];