简体   繁体   English

如何暂停应用程序的执行,直到iPhone发出Web服务的响应

[英]How to pause execution of app until the response of a web service comes in iPhone

I am working on an iPhone app in which I am calling a web service in my app for accomplishing the same I am creating a NSURLConnection the parsing of data is based upon the response of connection delegate's didReceiveResponse: method. 我正在开发一个iPhone应用程序,在其中我要在应用程序中调用Web服务以完成相同的工作。我正在创建NSURLConnection,数据的解析基于连接委托的didReceiveResponse:方法的响应。 I would like to halt the execution of my code until I get any response from my service but I am not sure how to do the same. 我想在我的服务收到任何响应之前停止执行代码,但是我不确定该怎么做。 Please let me know if you have solution. 如果您有解决方案,请告诉我。

Thanks Sandy 谢谢桑迪

In my opinion what you want to do is not halt the execution of your code (Apple won't be happy about it) but rather alert the user that you're waiting for something and disable your UI for anything you don't want the user to do in the meantime. 在我看来,您要做的不是停止执行代码(Apple对此不会感到满意),而是提醒用户您正在等待某些内容,并为您不需要的任何内容禁用UI。用户在此期间要做的事情。

You can construct a fairly standard and simple way for this, which go something like this: 您可以为此构建一个相当标准和简单的方法,如下所示:

  1. Create a UIView with a black background and set it's opacity to 0.8 - This will give users the disabled look. 创建一个黑色背景的UIView并将其不透明度设置为0.8-这将为用户提供禁用的外观。 And set the size of the view to cover your entire screen 并设置视图的大小以覆盖整个屏幕
  2. Add a UIButton with a custom style to fill your UIView - This will disable user clicks by catching them before they get to your other controls 添加具有自定义样式的UIButton来填充您的UIView-这将通过在用户进入其他控件之前捕获用户点击来禁用它们
  3. Add a UIActivityIndicatorView and start it. 添加一个UIActivityIndi​​catorView并启动它。

When you get the didReceiveResponse remove the "wait" view and go about with dealing with the response you got. 当您获得didReceiveResponse时,请删除“等待”视图,然后继续处理您得到的响应。

Sorry, I don't have the code right in front of me, but there is a blocking NSUrlRequest. 抱歉,我前面没有代码,但是有一个阻塞的NSUrlRequest。 Search for blocking/NSURL/iPhone on google / iPhone doc's to get the full answer. 在Google / iPhone文档上搜索阻止/ NSURL / iPhone以获取完整答案。

只需同步使用NSURLConnection,即可搜索NSURLConnection Synchronous。

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

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