62

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?

zoul
  • 102,279
  • 44
  • 260
  • 354
Jordan
  • 21,746
  • 10
  • 51
  • 63

2 Answers2

130

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.

Pang
  • 9,564
  • 146
  • 81
  • 122
Tyler
  • 28,498
  • 11
  • 90
  • 106
  • The delegate does work and does respond when an error is sent by the web server. In this case the webserver sends nothing. Just spins. I configure the request method to add the timeout. Thanks – Jordan Sep 15 '09 at 02:48
  • If you want to try an alternative to NSURLStuff, a friend of mine recommended ASIHTTPRequest (google it) which is free to use, and looks pretty friendly, including timeout functionality. – Tyler Mar 30 '11 at 21:31
  • 39
    fyi, default is 60 seconds. – jpswain Apr 29 '12 at 23:49
  • If i put flight mode while http call with server. how do i know request failed ? any completion block? – Avijit Nagare Sep 14 '16 at 08:04
24

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.

NSMutableURLRequest not obeying my timeoutInterval

Community
  • 1
  • 1
ezekielDFM
  • 1,947
  • 13
  • 26
  • I believe I fixed this by add NSCredentials to my Application. The non responsiveness was due to the server requesting authentication, which I wasn't providing. – Jordan Aug 31 '10 at 22:52
  • 5
    That's wrong information! According to http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSURLRequest_Class/Reference/Reference.html%23//apple_ref/occ/cl/NSURLRequest the default timeout interval is 60 seconds. – stigi Dec 10 '10 at 09:27
  • 4
    @stigi it looks like you're referencing material for Mac OS whereas ezekielDFM is talking about iOS. – leukosaima Dec 13 '10 at 19:49
  • 1
    @leukosaima The documentation for the iOS version of `NSUrlRequest` (just replace 'mac' with 'ios' in the URL) also states that the default is 60 seconds (although I don't know whether this was the case at the time that stigi and you posted, nor whether what the documentation claims was/is actually true). – Mark Amery Jul 31 '13 at 16:17
  • -1; this answer is indeed obsolete. The 240 second minimum value for the `timeoutInterval` property is no longer in place. – Mark Amery Sep 06 '13 at 14:56