Cheat Sheet

iOS Cloud Development For Dummies

From iOS Cloud Development For Dummies by Neal Goldstein

When you talk about iOS cloud programming, you are talking about two things: iCloud (which is implemented for you by Apple and allows you to synch data between devices) and web services (which enable you to have some of your app's processing done by a remote computer somewhere in the middle of who knows where). Web services rely on efficient ways of handing web service requests — requests made by a user for data stored in the cloud. The two most efficient formats (for now, at least) are XML and JSON. The tips you'll find here show you how to set up a web request (using the Google Geocoder web service as an example) and how to parse both the XML response and the JSON response so you can use the web service data in your application.

Parsing Data Returned from the Cloud by a Web Service

When you use a web service that's based in the cloud, your response is likely to be in JSON or XML, two standard formats for transmitting data between a server and web application. There will be times when you have a choice between the two formats, but other times you have to go with what you get.

The Google Geocoding Web Service, for example, does give you a choice.

If you want the response in XML format, you would use

http://maps.googleapis.com/maps/api/geocode/xml

If you wanted the response in JSON, you would use

http://maps.googleapis.com/maps/api/geocode/json?paremters

While similar in intent, how you get information out of XML and JSON data structures is quite different in execution.

Parsing an XML Response from the Cloud

When you use a web service that's based in the cloud, your response might be in XML. If, when working with your cloud-based web service, you were to enter the following in your web browser

http://maps.googleapis.com/maps/api/geocode/xml?sensor=true&address=Empire
State Building&

you'd get back some XML — this, for example:

…
<geometry>
 <location>
  <lat>40.7484395</lat>
  <lng>-73.9856709</lng>
</location>
…

Since XML doesn't exactly express itself in ways that are easy to understand, you would then have to parse whatever it is that XML gives you.

Remember XML is made up of elements that have start and end tags. To get at the information through iCloud, you use the NSXMLParser class that uses the delegation pattern. Here's how that would work:

NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithData:data];
[xmlParser setDelegate:self];
[xmlParser parse];

It turns out that there are four NSXMLParser delegate methods you will be implementing to parse an XML data structure:

parser:didStartElement:namespaceURI:qualifiedName:attributes:
parser:foundCharacters:
parser:didEndElement:namespaceURI:qualifiedName:
parser:parseErrorOccurred:

parser:didStartElement:namespaceURI:qualifiedName:attributes: lets you know that the parser has found an element with a tag, <lat> for example, and it's up to you to extract the data in the parser:foundCharacters: method.

Parsing a JSON Response from the Cloud

When you use a web service that's based in the cloud, your response might be in JSON. If, when working with your cloud-based web service, you were to enter the following in your web browser

http://maps.googleapis.com/maps/api/geocode/json?sensor=true&address=Empire 
State Building&

you'd get back some JSON — the following, for example

result {
results: [
  {
   …
   geometry {{
      bounds: {
       northeast: {
       lat: 40.7489545,
       lng: -73.98483449999999
       },
       southwest: {
       lat: 40.7479245,
       lng: -73.9865073
                         }
       },
     location: {
       lat: 40.7484395,
       lng: -73.9856709
  },
…
           }

Since JSON is just as cryptic as XML, you would then have to parse whatever it is that JSON gives you.

Parsing JSON, however, is quite different from parsing XML. JSON is built upon two structures you are quiet familiar with — arrays and dictionaries. The following JSON example uses both:

NSError *error = nil;
NSDictionary *responseDict = [NSJSONSerialization JSONObjectWithData:data 
options:NSJSONReadingMutableContainers error:&error];

When you do the JSON parsing, you have few options you can set as well:

  • NSJSONReadingMutableContainers: The arrays and dictionaries you get back are mutable so you can modify them. The default is immutable.

  • NSJSONReadingMutableLeaves: The strings you get back are mutable so you can modify them. The default is immutable.

  • NSJSONReadingAllowFragments: Specifies that the parser should allow top-level objects that are not arrays or dictionaries. The default requires the JSON top-level objects to be either an arrays or dictionaries.

When you use the NSJSONSerialization class, you (usually but not necessarily) get back either an array or dictionary that can include strings and numbers and even other arrays and dictionaries (think property list objects here).

In this case, you could get at the location lat and lng by doing

NSDictionary *result = [results objectAtIndex:0];
NSDictionary *geometry = [result objectForKey:@"geometry"];
NSDictionary *location = [geometry objectForKey:@"location"];
latitude = [location valueForKey:@"lat"];
longitude  = [location valueForKey:@"lng"];
blog comments powered by Disqus
Advertisement

Inside Dummies.com