Question

Just wondering about this code below... when I turn off my internet connection and run it, I expected I would get "Connection failed" in my console log. Can anyone explain why I'm not? Thanks.

NSString *urlString = [NSString stringWithFormat:@"http://www.myurl.com/RSS/feed.xml"];

NSURL *serviceURL = [NSURL URLWithString:urlString];

//Create the request
NSURLRequest *request = [NSURLRequest requestWithURL:serviceURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30];

//Create the connection and send the request
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];


//Make sure the connection is good
if (connection) {
    //instantiate the responseData structure to store the response
    self.responseData = [NSMutableData data];

}
else {
    NSLog(@"Connection failed");
}
Was it helpful?

Solution

You haven't actually attempted to make the request yet. if (connection) doesn't test if the request was successful, it only tests whether or not you were able to create the object representing the connection. You still need to call one of the methods on it to make the request. See the documentation for details.

OTHER TIPS

You're wanting to check to see if the connection itself failed, not the creation of the connection object, use the delegate, like so:

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
        NSLog("Oh noes D=");
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top