如何在 iPhone 中的 NSURLRequest 中设置超时

发布于 2024-10-11 12:31:17 字数 106 浏览 4 评论 0原文

当互联网连接断开时,我尝试在 15 秒内断开连接或停止加载 URL 请求。我尝试在 NSURLRequest 中使用 setTimeoutInterval,但它不起作用。有谁知道如何解决这个问题吗?

I'm trying to disconnect or stop loading URL request in 15 secs, when internet connection get disconnected. i tried with setTimeoutInterval in NSURLRequest, which is not working. can any know how to resolve the issue?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

墨小沫ゞ 2024-10-18 12:31:17

下面是一个示例:

NSURLRequest *lRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:link]         
               cachePolicy:NSURLRequestUseProtocolCachePolicy  
               timeoutInterval:20.0];

这里有 20 秒的超时间隔。

Here is an example for you:

NSURLRequest *lRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:link]         
               cachePolicy:NSURLRequestUseProtocolCachePolicy  
               timeoutInterval:20.0];

Here you have a 20 sec timeout interval.

隔纱相望 2024-10-18 12:31:17

根据我的经验,奥列格的这个例子行不通。我想您面临着与我相同的问题,并且从我在网上阅读的内容来看,也许这个解决方案也适合您。

使用 NSTimer,将计时器设置为在 webViewDidStartLoad 委托方法处启动,然后确保 webViewDidFinishLoad 和 webViewDidStarLoad 方法上有计数器来检查 webView 是否已加载(这是一个丑陋的 hack,但除非你使用 javascript 来检查页面中元素的数量,确实无法知道这两个方法将被调用多少次)。

执行此操作后,在计时器到达时间后检查您正在运行的方法中的计数器,这应该可以工作。

NSTimer 代码在这里 --> http://www.iphonedevsdk.com/forum/iphone -sdk-development/14403-nstimer-examples.html

希望这有帮助,它对我有用。尽管我知道存在缺陷,但它对我有用,因为我正在加载一个本来应该是轻量级的启动屏幕。不过,我仍在寻找解决方案,如果我有其他选择,我会在这里发布编辑。

非常欢迎反馈!

From what I have experienced, this example from Oleg will not work. I guess you're facing the same problem as me and from what I have read online, maybe this solution will work for you too.

Using NSTimer, set the timer to start at the webViewDidStartLoad delegate method, and then make sure that you have counters at the webViewDidFinishLoad and webViewDidStarLoad methods to check if the webView has been loaded (this is an ugly hack, but unless you use javascript to check the number of elements in your page, there is really no way to know how many times these 2 methods will be called).

After doing so, do a check of the counters at the method that you are running after the timer has reached its time, and that should work.

The NSTimer code is here --> http://www.iphonedevsdk.com/forum/iphone-sdk-development/14403-nstimer-examples.html

Hope this helps, it did for me. Although I know there are flaws, it works for me as I am loading a splash screen which is supposed to be lightweight in the first place. I'm still looking for solutions though, will post edits here if I have other alternatives.

Feedback is greatly welcomed!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文