NSURLConnection timeout?
Asked Answered
N

2

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?

Nickelic answered 15/9, 2009 at 0:51 Comment(0)
K
129

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.

Kisser answered 15/9, 2009 at 2:13 Comment(4)
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. ThanksNickelic
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.Kisser
fyi, default is 60 seconds.Archivolt
If i put flight mode while http call with server. how do i know request failed ? any completion block?Apices
F
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

Framework answered 31/8, 2010 at 17:2 Comment(5)
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.Nickelic
That's wrong information! According to developer.apple.com/library/mac/#documentation/Cocoa/Reference/… the default timeout interval is 60 seconds.Dansby
@Dansby it looks like you're referencing material for Mac OS whereas ezekielDFM is talking about iOS.Fluorocarbon
@Fluorocarbon 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).Channing
-1; this answer is indeed obsolete. The 240 second minimum value for the timeoutInterval property is no longer in place.Channing

© 2022 - 2024 — McMap. All rights reserved.