Database Reference
In-Depth Information
{"postalcodes":
[
{
"adminName2":"Rockingham",
"adminCode2":"015",
"postalcode":"03038",
"adminCode1":"NH",
"countryCode":"US",
"lng":-71.301971,
"placeName":"Derry",
"lat":42.887404,
"adminName1":"New Hampshire"
}
]
}
Basically, it's an array of postal code records, with a name of fields inside it. To see how
we can access this data in iOS, we'll add another button to the tab bar, and add the
code shown in Example 4-10 to the controller.
Example 4-10. Making a request that returns JSON
- (IBAction)lookupZipCode:(id)sender {
NSURL *url = [WebServiceEndpointGenerator getZipCodeEndpointForZipCode:@"03038"
country:@"US"
username:@"buggywhipco"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue currentQueue]
completionHandler:^(NSURLResponse *response,
NSData *data, NSError *error) {
if (error != nil) {
[self handleError:error.description];
} else {
[self gotZipCode:data];
}
}];
}
- (void)gotZipCode:(NSData *)data
{
NSError *error;
NSDictionary *json =
[NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
if (error != nil) {
[self handleError:error.description];
return;
}
NSArray *postalCodes = [json valueForKey:@"postalcodes"];
if ([postalCodes count] == 0) {
NSLog(@"No postal codes found for requested code");
return;
}
for (NSDictionary *postalCode in postalCodes) {
NSLog(@"Postal Code: %@", [postalCode valueForKey:@"postalcode"]);
 
Search WWH ::




Custom Search