如何在 iPhone 中的 NSURLRequest 中设置超时
当互联网连接断开时,我尝试在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
下面是一个示例:
这里有 20 秒的超时间隔。
Here is an example for you:
Here you have a 20 sec timeout interval.
根据我的经验,奥列格的这个例子行不通。我想您面临着与我相同的问题,并且从我在网上阅读的内容来看,也许这个解决方案也适合您。
使用 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!