Database Reference
In-Depth Information
NSArray *times = [timeDict valueForKey:timeLayout];
for (DDXMLElement *value in values) {
NSString *val = [value stringValue];
NSLog(@"Type: %@, Units: %@, Time: %@",
type, units, [times objectAtIndex:i]);
NSLog(@"Name: %@, Value: %@",
name, val);
NSLog(@" ");
i++;
}
}
return;
}
alert = [[UIAlertView alloc] initWithTitle:@"Error parsing XML"
message: [error localizedDescription]
delegate:self
cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
}
- (IBAction)showWeather:(id)sender {
NSURL *url =
[WebServiceEndpointGenerator
getForecastWebServiceEndpoint:@"03038"
startDate:[NSDate date]
endDate:[[NSDate date]
dateByAddingTimeInterval:3600*24*2]];
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 gotWeather:data];
}
}];
}
Let's take a good look at what's going on here. First off, we have the new handler for
the weather button we added to the tab bar, called showWeather . What this method does
is to construct an endpoint using the generator we just created. It passes in the zip code
for Derry, NH (my home sweet home), and start and end dates that correspond to now
and 24 hours from now. Then it does an asynchronous request to that URL using the
same completion handler mechanism that we used in the previous example.
One advantage that the NSURLConnection method has over my previous choice, ASI
HttpRequest , is that it is purely block-driven for the handlers. This can be significant
when you have multiple asynchronous requests in flight, because a block-based handler
lets you tie the handler directly to the request, rather than using selectors to specify the
delegates to handler the responses.
 
Search WWH ::




Custom Search