Database Reference
In-Depth Information
indicators) until the response returns. The pattern you want to use is to lock the UI
(typically by setting various UI elements to have userInteractionEnabled set false), and
then make an asynchronous call with both the success and failure callbacks re-enabling
the UI.
Thankfully, in iOS 5, the system-provided class was extended to include asynchronous
requests, so there's no longer a reason to avoid using it.
Using NSURLConnection—The BuggyWhip News Network
Let's see this package put through its paces, in this case by creating a page in the
BuggyWhipChat application that lets you see the latest news on buggy whips. Un-
fortunately, the server folks haven't gotten the news feed working yet, so we'll be using
CNN for testing. The page itself is going to be simple, a new button on the iPad toolbar
that gets the CNN home page, and displays the HTML in an alert. Yes, this is a totally
useless example, but we are talking about BuggyWhipCo here! Actually, the reason
that we're not doing more with the HTML is two-fold: there's a perfectly good UIWeb
View control that already does this, and the point is to highlight the NSURLConnection
package, not to display web pages.
First, we add the toolbar item to the detail XIB and hook it up the “Sent Action” for
the button to a new IBAction called showNews in the view controller, as shown in
Example 4-1 .
Example 4-1. A simple example using NSURLConnection
- (void)handleError:(NSString *)error{
UIAlertView *alert =
[[UIAlertView alloc]
initWithTitle:@"Buggy Whip News Error"
message: error
delegate:self cancelButtonTitle:@"OK"
otherButtonTitles:nil, nil];
[alert show];
}
-(IBAction) showNews:(id) sender {
NSURL *url = [NSURL URLWithString:@"http://www.cnn.com/index.html"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[NSURLConnection
sendAsynchronousRequest:request
queue:[NSOperationQueue currentQueue]
completionHandler:^(NSURLResponse *response,
NSData *data, NSError *error) {
if (error != nil) {
[self handleError:[error localizedDescription]];
} else {
[self displayResponse:data];
if ([response isKindOfClass:[NSHTTPURLResponse class]]) {
NSHTTPURLResponse *httpresp =
 
Search WWH ::




Custom Search