18

Does anyone know in which situations initializing a NSURLConnection returns nil instead of the created connection. The documentation says it's possible but fails to specify when this happens.

The method/message in question:

[[NSURLConnection alloc] initWithRequest:request delegate:self];

-

According to the NSURLConnection Class Reference:

Return Value: The URL connection for the URL request. Returns nil if a connection can't be initialized.

The URL Loading System Programming Guide says the following:

If NSURLConnection can’t create a connection for the request, initWithRequest:delegate: returns nil.

-

While it's possible that this method returns nil, I'm unable to come up with a scenario which triggers this. I've tried the following scenarios:

  • URLRequest with an empty url: connection:didFailWithError: delegate method is called with "unsupported URL" as error.
  • URLRequest with invalid url: connection:didFailWithError: delegate method is called with "bad URL" as error.
  • URLRequest with nonexistent url: connection:didFailWithError: delegate method is called with "A server with the specified hostname could not be found." as error.
  • Valid request but no internet: connection:didFailWithError: delegate method is called with "The Internet connection appears to be offline." as error.
  • nil request: causes a crash.

The initWithRequest method returned a valid NSURLConnection in each scenario (besides the last one) and called the connection:didFailWithError: with an appropriate error.

Has anybody been able to figure out which scenario does cause nil to be returned?

ynnckcmprnl
  • 4,382
  • 1
  • 24
  • 25

5 Answers5

1

I believe this can also be used when it fails to load, not just initialize. (The alloc is done separately - that's where low mem would probably bite you) So (I'm guessing) it could fail because you did not have a network available (3G/Wifi) or it simply failed to connect to the server. In any event, use:

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error

To get the actual failure.

You may be able to simulate this with an absence of a network - or even giving it a bad URL.

Brad
  • 11,262
  • 8
  • 55
  • 74
  • I have tested all scenarios you've mentioned, like I've mentioned in the question. No network connection/bad URL indeed call connection:didFailWithError. But that's not the question, I want to know when initializing fails. Guess I'll have to try to simulate a low memory situation. – ynnckcmprnl Oct 25 '10 at 07:24
  • If it returns nil, there will be no delegate callbacks. – BTRUE Jul 29 '15 at 17:59
1

I guess the answer is "Never". Seems only way for NSURLConnection to return nil is failing at [super init]. ([super init] returning nil) But as super class of NSURLConnection is NSObject and NSObjects init just returns self (never nil)

PS: That's for IOS SDK 4.0, on emulator, can be different on device.

Deniz Mert Edincik
  • 4,336
  • 22
  • 24
  • This is consistent with it being an "init*" method - it's conforming to the general rule for `init` in NSObject subclasses: "Subclass implementations of this method should initialize and return the new object. If it can’t be initialized, they should release the object and return nil." – David Gelhar Oct 30 '10 at 02:06
  • No, I've had it return nil before. – BTRUE Jul 29 '15 at 17:58
0

NSURLConnection returns nil if it is not created inside a runLoop.

BTRUE
  • 390
  • 3
  • 8
0

I would try all of the above except do it during low memory conditions. IE, I think it will happen when an internal malloc fails.

greg
  • 61
  • 1
  • 3
0

I thing this is caused if the request u specified has wrong or not all essential values

Naveen Shan
  • 9,192
  • 3
  • 29
  • 43