1

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");
}
sayguh
  • 2,540
  • 4
  • 27
  • 33

2 Answers2

1

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.

Jim
  • 72,985
  • 14
  • 101
  • 108
  • Hmm.. can you tell me how I can test if it was successful? I tried if ([NSURLConnection canHandleRequest:request]) but that didn't seem to work either – sayguh Mar 22 '12 at 13:19
  • Assuming you are sending an asynchronous request, you need to implement the delegate method `connection:didFailWithError:`. – Jim Mar 22 '12 at 14:53
  • Thanks for the info Jim. re:comments below.. the didReceiveData seems to work even with WiFi turned off. So that's what I meant about caching. I see now that it receives data even if the page returns a 404. Can you tell me what the common way is to make sure you have returned the full file you were looking for? – sayguh Mar 22 '12 at 16:39
  • 1
    `NSHTTPURLResponse` has a `statusCode` property, which corresponds to the HTTP response status. 200 is the usual value for a successful response, 404 is the usual value for a missing document. There are other possibilities as well. You should refer to [RFC 2616](http://tools.ietf.org/html/rfc2616) to see what the standard codes are. – Jim Mar 22 '12 at 16:56
1

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=");
}
Patrick T Nelson
  • 1,234
  • 12
  • 21
  • I implemented this delegate method as you suggested... but it's still not firing with the internet turned off? – sayguh Mar 22 '12 at 13:26
  • Well I see in you're code that you are indeed setting the delegate to self, so if that function doesn't fire, I couldn't tell you what the issue is.. are you also implementing the other delegate methods? (didFinishLoading and didRecieveData) ONE of the 3 should fire no matter what! – Patrick T Nelson Mar 22 '12 at 13:28
  • WEIRD! I implemented didReceiveData like you suggested. I run the app and it fires. Then I turn off WiFi (and verify I can't reach any websites) run the app again and it still fires?? *confused* – sayguh Mar 22 '12 at 13:53
  • Perhaps its still making a connection through 3G. To test this, put your phone in airplane mode. – Patrick T Nelson Mar 22 '12 at 13:54
  • Just using the simulator on my MacBook. No 3G – sayguh Mar 22 '12 at 13:57
  • The mystery really continues... I am beyond confused at the moment. If I change urlString to "http://www.google.com/rss/feed.xml" (a URL that doesn't exist) didReceiveData fires. If I change "http://www.ffdssssf.com/rss/feed.xml" I get "Oh noes". So it only seems to care if the domain exists and not the URL. And then the mega weird part is that the same thing happens if my internet is on or off – sayguh Mar 22 '12 at 14:15
  • Also, why does the Apple documentation no NSURLConnectionDelegate protocol not mention didReceiveData or didFinishLoading methods? http://developer.apple.com/library/mac/#documentation/Foundation/Reference/NSURLConnectionDelegate_Protocol/Reference/Reference.html#//apple_ref/occ/intf/NSURLConnectionDelegate – sayguh Mar 22 '12 at 14:25
  • Simulator does some type of caching I guess – sayguh Mar 22 '12 at 14:55
  • @sayguh, if there isn't a document there, the connection is still successful. You're still connecting to the web server, sending a request and receiving a response, even if that response is telling you that it doesn't have a document at that location. If a hostname doesn't exist, you can't make a connection because there's no server to connect to, so it fails. Nothing to do with caching. – Jim Mar 22 '12 at 15:00