views:

110

answers:

1

I have the following code:

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.lenzerheide.com/modules/weather/iphoneweather.php"] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval: 10.0f];

[[NSURLConnection alloc] initWithRequest:request delegate:self];

When I test the app on an iPhone, on some phones (probably those that haven't visited a page in mobile safari recently) the didFailWithError gets called with "the connection was terminted", not once, but always. But when I visit a site in mobile safari, this problem seems to go away completely for a few hours. I can even delete the app, reinstall it and the problem is still gone. It seems to take a few hours till this problem comes back up and than it's the same procedure, it only loads the XML after I visited a site in mobile safari first. If I don't do that, didFailWithError gets called all the time.

Oh and btw, the webpage hosting the xml is always working. I can load the XML on my Mac without any problems or even lags. Just on iPhones that haven't visited a page in mobile safari recently, I see the problem.

Any ideas?

A: 

I think your timeout is fired earlier before page opens, when you first time try to visit the page, the DNS resolution on mobile may be taking little longer then expected, when you visit first time in mobile safari, then also dns for first time will take little longer but mobile safari may have little higher timeout then yours. And next time you visit the page from your app, the dns resolution will not take any time as it will pickup from cache for little timeout usually 2 to 48 hours (depending upon dns server's timeout value). So after visiting page in mobile safari for timeout value your xml may load faster and in your timeout value.

So try increasing your timeout value or ignore timeout value all together and then see.

Akash Kava
Thx, will give it a try. I first used the NSURLRequest requestWithURL call without the timeout option, but that had the same problem, so I'll try it now with a bigger timeout. Let's hope it works.
FrediBach
Ok, I changed it to 60 seconds, but no luck, the connection terminates almost immediately, so the timeout doesnt seem to have an influence. And like before, just visiting a page in mobile safari solves the problem. Weird.
FrediBach
Are you using synchronous connection?
Akash Kava
No, asynchronous.
FrediBach
Oh btw, the SimpleURLConnection sample code from the Apple site has the same problem, so I guess I can't do anything against it?
FrediBach