NSURLConnection超时? [英] NSURLConnection timeout?

查看:122
本文介绍了NSURLConnection超时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将NSURLConnection与委托人一起使用。

I'm using this NSURLConnection with delegates.

nsconnection = [[NSURLConnection alloc] initWithRequest:request
    delegate:self startImmediately:YES];

问题是网站根本没有回复。没什么,只是在浏览器中旋转空白页面,没有失败。在我的代表中,我处理失败,但当网站没有响应时,代表不会被调用。关于如何超时连接的任何想法?

Problem is the website doesn't respond at all. Nothing, just spins in browser with blank page, no failure. In my delegates, I handle the failure, but when the site doesn't respond the delegates don't get called. Any ideas on how to timeout the connection?

推荐答案

您可以在 NSURLRequest中指定超时对象。一种方法是通过 requestWithURL来构建它。 :cachePolicy:timeoutInterval: 方法。 (如果您不想担心该部分,可以传入默认的 NSURLRequestUseProtocolCachePolicy cachePolicy参数。)超时是一个以秒为单位的浮点值,基本上是iPhone SDK中的所有时间间隔。

You can specify a timeout in your NSURLRequest object. One way to do this is to construct it via the requestWithURL:cachePolicy:timeoutInterval: method. (You can pass in the default NSURLRequestUseProtocolCachePolicy cachePolicy parameter if you don't want to worry about that part.) The timeout is a floating-point value in seconds, as are basically all time intervals in the iPhone SDK.

还要确保设置 NSURLConnection 的委托并响应 connection:didFailWithError: 方法。连接总是在连接完成时调用此方法或 connectionDidFinishLoading:

Also make sure your NSURLConnection's delegate is set and responds to the connection:didFailWithError: method. A connection always calls either this method or connectionDidFinishLoading: upon connection completion.

这篇关于NSURLConnection超时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆