NSURLConnection的initWithRequest什么时候返回nil
有谁知道在什么情况下初始化 NSURLConnection
返回 nil 而不是创建的连接。文档说这是可能的,但没有具体说明这种情况何时发生。
有问题的方法/消息:
[[NSURLConnection alloc] initWithRequest:request delegate:self];
-
根据 NSURLConnection
类参考:
返回值: URL 请求的 URL 连接。如果连接无法初始化,则返回 nil。
URL 加载系统编程指南中有如下内容:
如果 NSURLConnection 无法为请求创建连接,则 initWithRequest:delegate: 返回 nil。
-
虽然此方法可能返回 nil,但我无法想出触发此问题的场景。我尝试过以下场景:
URLRequest
带有空 url:connection:didFailWithError:
委托方法被调用,并出现“不支持的 URL”错误。URLRequest
with invalid url:connection:didFailWithError:
委托方法被调用,错误为“bad URL”。- url 不存在的
URLRequest
:connection:didFailWithError:
委托方法被调用,并显示“无法找到具有指定主机名的服务器”。作为错误。 - 请求有效,但没有互联网:
connection:didFailWithError:
调用委托方法时显示“互联网连接似乎已离线”。作为错误。 - nil 请求:导致崩溃。
initWithRequest
方法在每种情况下(除了最后一种情况)都返回一个有效的 NSURLConnection
,并调用 connection:didFailWithError:
并给出相应的错误。
有人能够弄清楚哪种情况会导致 nil 返回吗?
Does anyone know in which situations initializing a NSURLConnection
returns nil instead of the created connection. The documentation says it's possible but fails to specify when this happens.
The method/message in question:
[[NSURLConnection alloc] initWithRequest:request delegate:self];
-
According to the NSURLConnection
Class Reference:
Return Value: The URL connection for the URL request. Returns nil if a connection can't be initialized.
The URL Loading System Programming Guide says the following:
If NSURLConnection can’t create a connection for the request, initWithRequest:delegate: returns nil.
-
While it's possible that this method returns nil, I'm unable to come up with a scenario which triggers this. I've tried the following scenarios:
URLRequest
with an empty url:connection:didFailWithError:
delegate method is called with "unsupported URL" as error.URLRequest
with invalid url:connection:didFailWithError:
delegate method is called with "bad URL" as error.URLRequest
with nonexistent url:connection:didFailWithError:
delegate method is called with "A server with the specified hostname could not be found." as error.- Valid request but no internet:
connection:didFailWithError:
delegate method is called with "The Internet connection appears to be offline." as error. - nil request: causes a crash.
The initWithRequest
method returned a valid NSURLConnection
in each scenario (besides the last one) and called the connection:didFailWithError:
with an appropriate error.
Has anybody been able to figure out which scenario does cause nil to be returned?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我相信这也可以在加载失败时使用,而不仅仅是初始化。 (分配是单独完成的 - 这就是低内存可能会咬你的地方)所以(我猜)它可能会失败,因为你没有可用的网络(3G/Wifi)或者它只是失败了连接到服务器。无论如何,请使用:
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
来获取实际的失败情况。
您也许可以在没有网络的情况下模拟这一点 - 或者甚至给它一个错误的 URL。
I believe this can also be used when it fails to load, not just initialize. (The alloc is done separately - that's where low mem would probably bite you) So (I'm guessing) it could fail because you did not have a network available (3G/Wifi) or it simply failed to connect to the server. In any event, use:
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
To get the actual failure.
You may be able to simulate this with an absence of a network - or even giving it a bad URL.
我想答案是“从不”。似乎 NSURLConnection 返回 nil 的唯一方法是在 [super init] 处失败。 ([super init] 返回 nil)但是,由于 NSURLConnection 的超类是 NSObject,而 NSObjects init 只是返回 self(从不为 nil)
PS:这适用于 IOS SDK 4.0,在模拟器上,在设备上可能有所不同。
I guess the answer is "Never". Seems only way for NSURLConnection to return nil is failing at [super init]. ([super init] returning nil) But as super class of NSURLConnection is NSObject and NSObjects init just returns self (never nil)
PS: That's for IOS SDK 4.0, on emulator, can be different on device.
除了在内存不足的情况下进行之外,我会尝试上述所有方法。 IE,我认为当内部 malloc 失败时会发生这种情况。
I would try all of the above except do it during low memory conditions. IE, I think it will happen when an internal malloc fails.
我认为这是因为您指定的请求有错误或不是所有基本值造成的
I thing this is caused if the request u specified has wrong or not all essential values
如果 NSURLConnection 不是在 runLoop 内创建的,则它返回 nil。
NSURLConnection returns nil if it is not created inside a runLoop.