Tôi có một số NSURLConnection
trong phân lớp ô xem bảng có thể tải xuống hầu hết các tệp. Tôi nhận thấy, tuy nhiên, một số không bắt đầu tải xuống, và thời gian ra ngoài. Một ví dụ sẽ là URL this, đây chỉ là tệp zip thử nghiệm tải xuống tốt trong bất kỳ trình duyệt nào khác. Heres mã của tôi để tải xuốngiOS NSURLConnection không tải xuống các tệp từ một số URL nhất định
-(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);
}
Bất kỳ ai thấy điều gì có thể gây ra điều này?
Heres lỗi:
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."}
Triển khai phương thức giao thức 'connection: didFailWithError:' và xem bạn gặp phải lỗi gì. – Malloc
@Malloc xấu của tôi, tôi quên đăng bài đó. Nó chỉ nói rằng kết nối đã hết thời gian chờ. –
Ok, vậy bạn gặp phải lỗi gì? – Malloc