【发布时间】:2013-04-30 22:28:22
【问题描述】:
我在可以下载大多数文件的 tableview 单元子类中有一个 NSURLConnection。但是,我注意到有些无法开始下载并超时。一个例子是this URL,它只是一个测试 zip 文件,可以在任何其他浏览器中正常下载。这是我的下载代码
-(void)downloadFileAtURL:(NSURL *)url{
self.downloadedData = [[NSMutableData alloc] init];
self.url = url;
conn = [[NSURLConnection alloc] initWithRequest:[NSURLRequest requestWithURL:self.url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:1200.0] delegate:self startImmediately:YES];
}
- (void)connection:(NSURLConnection*)connection didReceiveResponse:(NSHTTPURLResponse*)response
{
int statusCode = [response statusCode];
if (statusCode == 200){
self.fileName.text = response.URL.lastPathComponent;
self.respo = response;
expectedLength = [response expectedContentLength];
}
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
[self.downloadedData appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
CFStringRef mimeType = (__bridge CFStringRef)[_respo MIMEType];
CFStringRef uti = UTTypeCreatePreferredIdentifierForTag(kUTTagClassMIMEType, mimeType, NULL);
CFStringRef extension = UTTypeCopyPreferredTagWithClass(uti, kUTTagClassFilenameExtension);
NSString *fileName = [NSString stringWithFormat:@"%@.%@", [[_respo suggestedFilename] stringByDeletingPathExtension], (__bridge NSString *)extension];
[[NSFileManager defaultManager] createFileAtPath:[[self docsDir] stringByAppendingPathComponent:[NSString stringWithFormat:@"Downloads/%@", fileName]] contents:_downloadedData attributes:nil];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
NSLog(@"Download failed with error: %@", error);
}
有人看到可能导致这种情况的任何东西吗?
这是错误:
Error Domain=NSURLErrorDomain Code=-1001 "The request timed out." UserInfo=0x1fd2c650
{NSErrorFailingURLStringKey=http://download.thinkbroadband.com/10MB.zip,
NSErrorFailingURLKey=http://download.thinkbroadband.com/10MB.zip,
NSLocalizedDescription=The request timed out., NSUnderlyingError=0x1fdc90b0 "The request timed out."}
【问题讨论】:
-
实现
connection:didFailWithError:协议方法,看看你得到了什么错误。 -
@Malloc 我的错,我忘记发布了。它只是说连接超时。
-
好的,那么你得到的错误堆栈是什么?
-
您尝试从同一服务器下载的所有文件吗?超时错误纯粹是服务器端。
-
我的问题中的链接在 Safari 上下载,而不是在我的手机上。这就是为什么我认为它与我的下载有关,因为它也发生在其他文件上。
标签: ios objective-c nsurlconnection