NSURLConnection hangs with invalid URL

Go To StackoverFlow.com

2

I'm creating an NSURLConnection but using an invalid URL (http://thiswontwork.com/asdf). I'm calling it on the main thread and I've implemented all of the delegate methods in my delegate.

The connection hangs and never calls any of the delegate methods, I would have expected connection:didFailWithError: to be called. The only delegate method called is

- (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse

Has anyone come across anything like this?

Happens under debugger on device and in simulator (iOS5 on xCode4).

Have created a trivial view controller with single button example to demonstrate, can post code if required.

Timeout interval is being set on the request (to 30 secs).

2012-04-04 04:31
by gamozzii
how you are creating connection, it seems like problem is somewhere else, can you add that code also - rishi 2012-04-04 04:37
Make sure the delegate methods which aren't getting called are spelled correctly - Costique 2012-04-04 04:40
will post code later, but rather strangely if I remove the delegate method highlighted above then the other delegate methods are called with success response codes. I've got some other (non-trivial) code that also has strange hangs in NSURLConnection. Its an old project in subsequent projects I've been using another library - gamozzii 2012-04-04 04:46
What are you returning in that method when it's called - NJones 2012-04-04 04:46
was returning nil in that method NJones - gamozzii 2012-04-04 04:47


1

If you return nil the connection is finished. I'm not in front of the docs right now but I believe it's standard practice to return the NSURLRequest passed into the function. Something like:

-(NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response{
    return request;
}

Edit

After looking at the documentation I see what you mean. The documentation is extremely misleading as to what will happen if you return nil. It says if you return nil the connection will continue but I believe that this is only in the case where response is not nil. I tested returning nil in a small download manager project I had written and it seemed to stall the connection out completely.

This would seem to either be a bug or an instance of poor documentation.

2012-04-04 04:50
by NJones
Except that the doc implies (and my experience seems to be) that the method can be called even when its not a real redirect, so you need to check the response - if the response is nil as I understand it it is ok to return nil - gamozzii 2012-04-06 00:03
@gamozzii I see what you mean. Edited in response - NJones 2012-04-08 03:47
Ads