I am using NSURLConnection to load data from server. So, in my Iphone Application had a button, when click on this button and internet connection is connected, the data will be loaded successfully. But later when I switched off the internet connection on Mac, and then click on that button again, the didFailWithError method does not invoked but other methods such as didReceiveResponse, connectionDidFinishLoading was get called.
Anyone encountered this kind of problem before? Or anyone know the reason of this problem?
Really appreciate for any comments, suggestions and solutions. Thanks.
Before changing any line in your code, try the same test with 4.3 simulator and then 5.0.
I've tried something similar (request to URL and connection is closed with no response), and using 4.3, didFailWithError is called. In 5.0, I get a didReceiveResponse with a status code 200 OK (!) and then connectionDidFinishLoading. Same code, same request, different OS versions...
Strange behavior especially because you turned internet off, but delegate still called for ** connectionDidFinishLoading** how this is possible? Also you wrote what "when click on this button and internet connection is connected" your code still initiated connection without internet?
It's possible, what you configured using of cached data and thats why you got strange behavior like this.
*nix systems by default have timeout for connection on BSD sockets which may don't tell you nothing about what connection is missed. But i think Mac OS/iOS configured for this kind of case.
Related
I am using NSURLConnection to communicate with a web service on my LAN. When using the Xcode Simulator everything works as expected. However, when using an iPhone the connection mostly times out (but not always).
The iPhone is connected to the LAN via wifi. I can consistently browse the web server (which is hosting the web service) using Safari.
However, the app mostly fails. It returns in the didFailWithError function reporting "The request timed out".
The app will intermittently succeed (behaving exactly as expected). I can see no pattern for success or failure. It may fail several times and then succeed and then fail again (without restarting the app). I estimate a 90%+ failure rate.
The web service always receives the request and always responds. It appears the failure happens with the device not receiving or handling the response.
I can see no obvious problem with my network. Certainly there is no IP address clash.
I am completely new to apps, Apple, Xcode and iPhone. So this could be something really very obvious to you. Any ideas what I could look at to resolve this?
The environment I'm currently using for development is as follows:
Mac OS X (10.7.4)
Xcode 4 (targetting 4.3)
iPhone 3GS (5.1.1)
Obviously I cannot give you the absolute answer, but can hopefully help you track the problem down.
First, you want vet your web service. Put a JPEG image in a public Dropbox folder. Now try to download that image with your code as well as view it with Safari. If Safari works but your code does not, then you know your code is the problem.
If both fail then the phone may have a bad transceiver, or your WIFI network may be flakey. Take the phone to some public WIFI location and redo the test. If both still fail on a second network, the phone is probably defective.
If in all cases Safari works but your code does not, then please post as much of your code as possible. Also, if using the asynchronous interface, are you getting redirected? Asked for credentials? Etc. I would be inclined to add every NSURLConnection delegate method and at least add a log so you know what's going on - that is log all responses. You can also get the html return code from the response:
assert([response isKindOfClass:[NSHTTPURLResponse class]]);
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
htmlStatus = [httpResponse statusCode];
if(htmlStatus != 200) NSLog(#"Server Response code %i", htmlStatus);
I'm having an issue that's incredibly hard to debug. If my app is using WiFi and it sits idle for around 30 minutes, sometimes when I wake it up, the NSURLConnection no longer responds. Requests are sent, but never return.
At first, I thought this was a memory bug with the instances being released too early and thus never returning a response. However, if I put the app in the background, go into settings, turn off WiFi so 3G is used exclusively, and return to the app, the internet connection magically comes back to life and all pending NSURLConnections return and complete.
Obviously this is an issue for people using WiFi. Is this really a WiFi issue, or am I missing something? Going to another app like Safari, using the WiFi radio and returning to my app doesn't solve the problem - connections still don't return. I've traced this with Xcode and I'm running out of ideas.
Also 'Application Uses WiFi' Info.plist flag is set to ON and this is firmware 5.1.
is 'Application Uses WiFi' the same as UIRequiresPersistentWiFi?
Update: This has nothing to do with the Wifi flag - it can die within 5 minutes. So far, I've only been able to duplicate it on my iPhone 4s with 5.1 firmware. It's not really a solution, but I'm erasing the phone to try it with a fresh install to see if that has any effect. I have verified that NSURLConnection is always called on the main thread, and set breakpoints at connection:failedWithError: and connection:didReceiveResponse:. When the connection dies, none of these return until I disable and re-enable WiFi, and then all return at once. This happens on a local server as well, and the server still returns if I ping it with a web browser.
For any others running into this issue, it's due to TestFlight v1.0 and below:
Why does NSURLConnection fail to reach the backend?
Are you actually transmitting/receiving any data through the NSURLConnection? If not, is it possible you are hitting a TCP session timeout? Seems unlikely if the problem persists on a local server, but any intervening stateful firewall/packet inspector might be casting off your TCP connection.
Every so often, my iPhone app gets into a state where network requests always time out, even if other apps work fine (and can even access the same sites). This isn't obviously correlated with changes in network availability, and happens both on 3G and over WiFi. Any suggestions on how to diagnose the problem?
(FWIW, the app uses MonoTouch and HttpWebRequest, but I suspect whatever's going wrong is lower-level.)
Note: The problem persists through backgrounding the app and changing the network configuration; the only fix seems to be to kill the app and re-launch it.
Updates: I've tried making use of Reachability, but to no effect. Reachability.InternetConnectionStatus always returns ReachableViaWiFiNetwork (or ReachableViaCarrierDataNetwork, depending; likewise IsHostReachable() always returns true. Runtime.StartWWAN() seems to make no difference.
There was a bug open about this at https://bugzilla.novell.com/show_bug.cgi?id=555439 and there were several attempts at resolving it. As of the last comment in the bug, it was presumed fixed but I guess if you are using MonoTouch 4.0.3 then there some cases that are not worked around.
Basically, the problem is reflected in this other Stackoverflow question: iPhone 3G Connection Enabling
What MonoTouch does to try and work around this issue is to call MonoTouch.Runtime.StartWWAN(Uri) which opens a dummy connection to the uri using an NSUrlConnection to force-wake the network interface. Then, MonoTouch goes back to using the BSD socket API inside the HttpWebRequest.
Try setting the timeOutInterval property of the NSURLRequest used to make the call.
My application allows the user to download relatively large files (~120 MB) from my own dedicated server. I'm using the ASIHTTPRequest library for downloading.
It may sound weird, but everything worked fine until yesterday. I've tried downloading files countless times both from my app (on an iPad) and a Mac, and while on the Mac the download succeeds, on the iPad it randomly times out. Sometimes it goes until 100%, sometimes it reaches 30%-40% or something and then ASIHTTPRequest's downloadFailed: selector gets called. If I print out the error's localizedDescription, I get "The request timed out".
What could this mean? Could it be a problem of my app? Or a problem with my server, or my connection? I realize this could depend on several factors, so please ask me any information you need if necessary. Thanks.
I had weird network situations like timeouts on my ipad when using wifi that started after the upgrade to 4.2, but were intermittent.
Going to Settings -> General -> Reset Network settings and then reentering the wifi settings solved it for me.
(Also check that someone hasn't recently setup a new nearby wifi access point on the same channel as your network!)
Does your server have limit about the number of connections ? I encountered a problem because the number of connections are too much, then the server declines those connections. Then, they are time out.
Do you use WiFi or 3G connection? Non-WiFi connection allows to download files with maximum size of 20Mb only.
I have an iPhone app which communicates with a server to get the data being displayed. I have tested this app on a wifi connection and a good 3G connection. The app works without an issue. But if I test the app on a poor connection, the app crashes.
I get an XML from the server and parse it before displaying the data. I have put in the NSXMLParser method to show an alert if the parsing fails. The n/w connection code is also placed in try/catch blocks and we show an alert if the control goes to the catch block.
On a poor internet connection, the app just crashes (doesn't even go to the catch block) and checking the crash logs suggests the app could not get the complete response. Shouldn't it go to the catch block in that case? (I am using a wrapper class to make a synchronous connection)
This will always be an issue in any app using the internet if the connection is poor. Is there any way we can avoid this?
Thanks.
I am using the code provided here as the base for creating connections and getting the response
This isn't a direct answer, but may I suggest the ASIHTTPRequest library? I searched around for a long time looking for a good networking library and this seemed to be pretty bullet-proof through all sorts of connection issues.
It took me an afternoon to remove the lousy library I wrote and to integrate it. The other nice thing about it is that it can be done asynchronously.
It is available here: http://allseeing-i.com/ASIHTTPRequest/