NSURLConnection timeout?

IphoneTimeoutNsurlconnection

Iphone Problem Overview


I'm using this NSURLConnection with delegates.

nsconnection = [[NSURLConnection alloc] initWithRequest:request
    delegate:self startImmediately:YES];

Problem is the website doesn't respond at all. Nothing, just spins in browser with blank page, no failure. In my delegates, I handle the failure, but when the site doesn't respond the delegates don't get called. Any ideas on how to timeout the connection?

Iphone Solutions


Solution 1 - Iphone

You can specify a timeout in your NSURLRequest object. One way to do this is to construct it via the requestWithURL:cachePolicy:timeoutInterval: method. (You can pass in the default NSURLRequestUseProtocolCachePolicy cachePolicy parameter if you don't want to worry about that part.) The timeout is a floating-point value in seconds, as are basically all time intervals in the iPhone SDK.

Also make sure your NSURLConnection's delegate is set and responds to the connection:didFailWithError: method. A connection always calls either this method or connectionDidFinishLoading: upon connection completion.

Solution 2 - Iphone

Edit: This answer is now obsolete, but at the date of answering was accurate.

I know this thread is old, but I ran into this issue as well. This happened when testing the app outside the internal network where the services lived. The service will eventually timeout, but Apple mandates a period of 240 seconds (4 minutes) before the time out occurs. Check out this thread for more info on the matter.

https://stackoverflow.com/questions/2736967/nsmutableurlrequest-not-obeying-my-timeoutinterval

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJordanView Question on Stackoverflow
Solution 1 - IphoneTylerView Answer on Stackoverflow
Solution 2 - IphoneezekielDFMView Answer on Stackoverflow