当我尝试使用以下代码连接到soundcloud时:
// Create the request.
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://soundcloud.com/api"] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:10.0];
// Create url connection and fire request
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
当我收到以下错误时:
Error Domain=NSURLErrorDomain Code=-1009 "The Internet connection appears to be offline." UserInfo={NSUnderlyingError=0x14cfccae0 {Error Domain=kCFErrorDomainCFNetwork Code=-1009 "The Internet connection appears to be offline." UserInfo={NSErrorFailingURLStringKey=https://soundcloud.com/api, NSErrorFailingURLKey=https://soundcloud.com/api, kCFStreamErrorCodeKey=50, kCFStreamErrorDomainKey=1, NSLocalizedDescription=The Internet connection appears to be offline.}}, NSErrorFailingURLStringKey=https://soundcloud.com/api, NSErrorFailingURLKey=https://soundcloud.com/api, kCFStreamErrorDomainKey=1, kCFStreamErrorCodeKey=50, NSLocalizedDescription=The Internet connection appears to be offline.}
关键是:我在iPhone上运行它,网络是4G。当我通过Wifi使用其他网络时,代码实际上运行良好。
答案 0 :(得分:8)
原因是我的iPhone上的应用程序关闭了4G网络,不知道我何时以及为什么这样做。
答案 1 :(得分:1)
我找到了解决方案,因为您设置了timeoutInterval:10.0 在
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://soundcloud.com/api"] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:10.0];
在这段时间内没有得到响应,因此您需要超过此时间,或者也可以删除此间隔