gpt4 book ai didi

objective-c - NSURLConnection 在禁用互联网连接的情况下没有给出错误?

转载 作者:行者123 更新时间:2023-11-29 11:15:06 24 4
gpt4 key购买 nike

只是想知道下面这段代码...当我关闭互联网连接并运行它时,我预计我会在控制台日志中看到“连接失败”。谁能解释为什么我不是?谢谢。

NSString *urlString = [NSString stringWithFormat:@"http://www.myurl.com/RSS/feed.xml"];

NSURL *serviceURL = [NSURL URLWithString:urlString];

//Create the request
NSURLRequest *request = [NSURLRequest requestWithURL:serviceURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30];

//Create the connection and send the request
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];


//Make sure the connection is good
if (connection) {
//instantiate the responseData structure to store the response
self.responseData = [NSMutableData data];

}
else {
NSLog(@"Connection failed");
}

最佳答案

您实际上还没有尝试发出请求。 if (connection) 不会测试请求是否成功,它只会测试您是否能够创建表示连接的对象。您仍然需要调用其中一种方法来发出请求。参见 the documentation了解详情。

关于objective-c - NSURLConnection 在禁用互联网连接的情况下没有给出错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9822812/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com