objective-ciosnsconnection

NSURLConnection not giving error with internet connection disabled?


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");
}

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.