1

I am having problems in my iphone application due to weak wifi signals. My application uses webservice to retireve data from our server but when Wifi signals are weak the response never comes back and user gets stuck on "Loading..." overlay screen. Finally the application crashes at the end. How can i handle this situation gracefully. Is there a way to set TimeOut for my webservice calls or something like this?

Thanks, Asif.

Asif
  • 21
  • 4
  • 9
  • 1
    How are you making these web service calls? NSURLConnection? `initWithContentsOfURL:` or the like on an NSString, NSDictionary, or the like? A third-party library? – Anomie May 18 '11 at 14:37

3 Answers3

0

You can set the NSURLConnection timeout and a delegate to respond to connection:didFailWithError: selector. See this S.O. topic.

if your delegate is never being called, this is a somehow known issue. The only workaround seems to be setting your own NSTimer to fire after some time and cancel the request. It is definitely awkard, but it should not be that complex.

If you are curious about the reason behind the issue with timeout, it seems to be related to the slow starting of the 3G subsystem in an iPhone.

Community
  • 1
  • 1
sergio
  • 68,819
  • 11
  • 102
  • 123
  • I have used WSDL2ObjC tool to consume web service in iphone app. WSDL2ObjC creats a class xMyService.m which contains the follwing code for NSURLConnection with request timeout 10 seconds But when WI-FI signals are weak, the request get stucked and connection timeout of 10 sec does not work. NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:self.address cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:10.0]; NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate: operation]; – Asif May 19 '11 at 13:59
  • @asif: so, have you tried to change the timeoutInterval value? Any improvements? – sergio May 19 '11 at 14:20
  • yes, I tried values less than 10, but that also does not work. and the application shows activity indicator for Indefinite time. – Asif May 20 '11 at 04:37
  • if your application shows activity indicator for indefinite time, this could be because you don't remove the activity indicator when the connection times out. What are you exactly doing in you `connection:didFailWithError:`? – sergio May 20 '11 at 07:41
  • The activity indicator is not removed in case the wi-fi signals are weak, and the response doesn't return back. In normal situation when wi-fi signal strength is good the application works properly and the activity indicator is removed just after the response is returned. – Asif May 20 '11 at 10:20
  • connection:didFailWithError looks like this. - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { //Network failure message is displayed here. if (binding.logXMLInOut) { NSLog(@"ResponseError:\n%@", error); } response.error = error; [delegate operation:self completedWithResponse:response]; } – Asif May 20 '11 at 10:35
  • Please note that in case of weak wi-fi signals the connection:didFailWithError never gets called. – Asif May 20 '11 at 13:29
0

try to use ASIHTTP lib

http://allseeing-i.com/ASIHTTPRequest/How-to-use

elp
  • 8,021
  • 7
  • 61
  • 120
0

You might want to learn about ASIHTTPRequest as it features much more than the standard CFNetwork api. The code is straight forward, error handling as well:

- (IBAction)grabURLInBackground:(id)sender
{
   NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com"];
   ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
   [request setDelegate:self];
   [request startAsynchronous];
}

- (void)requestFinished:(ASIHTTPRequest *)request
{
   // Use when fetching text data
   NSString *responseString = [request responseString];

   // Use when fetching binary data
   NSData *responseData = [request responseData];
}

- (void)requestFailed:(ASIHTTPRequest *)request
{
   NSError *error = [request error];
}
alex
  • 2,464
  • 23
  • 32