简体   繁体   English

iPhone Web服务请求超时

[英]iPhone Web service Request Timeout

I am having problems in my iphone application due to weak wifi signals. 由于无线信号较弱,我的iPhone应用程序出现问题。 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. 我的应用程序使用Web服务从服务器中检索数据,但是当Wifi信号较弱时,响应再也不会回来,并且用户会卡在“正在加载...”叠加屏幕上。 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. 谢谢,阿西夫。

You can set the NSURLConnection timeout and a delegate to respond to connection:didFailWithError: selector. 您可以设置NSURLConnection超时和connection:didFailWithError:以响应connection:didFailWithError:选择器。 See this SO topic. 请参阅此SO主题。

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. 唯一的解决方法似乎是将您自己的NSTimer设置为在一段时间后触发并取消请求。 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. 如果您对超时问题背后的原因感到好奇,那似乎与iPhone中3G子系统启动缓慢有关。

You might want to learn about ASIHTTPRequest as it features much more than the standard CFNetwork api. 您可能想了解ASIHTTPRequest,因为它具有比标准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];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM