ios - 应用程序不使用 NSURLConnection 下载文件

标签 ios objective-c cocoa-touch nsurlconnection

我有一个循环遍历一组 URL 并下载该数组中的所有文件(10 个小 txt 文件和 1 个大图像文件)的应用程序。我之前让应用程序下载文件没问题,但由于大图像的问题,我不得不将 NSURLConnection 子类化,我认为我没有完全正确,因为现在没有文件正在下载。

为数组 getFiles 中的每一项传递一个 URL 并运行:

-(void)getFile:(NSString*)url tag:(NSString*)tag {
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
    FileURLConnection *connection = [[FileURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES tag:tag];
    if (connection) {
        [receivedData setObject:[NSMutableData data] forKey:connection.tag];
    }
}

这是 NSURLConnection 代表:

-(NSMutableData*)dataForConnection:(FileURLConnection*)connection {
    NSMutableData *data = [receivedData objectForKey:connection.tag];
    return data;
}
- (void)connection:(FileURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    NSUserDomainMask, YES) objectAtIndex:0]stringByAppendingPathComponent:fileName];
    NSMutableData *dataForConnection = [self dataForConnection:(FileURLConnection*)connection];
    [dataForConnection setLength:0];
}
- (void)connection:(FileURLConnection *)connection didReceiveData:(NSData *)data {
    NSMutableData *dataForConnection = [self dataForConnection:(FileURLConnection*)connection];
    [dataForConnection appendData:data];
}
- (NSCachedURLResponse *)connection:(FileURLConnection *)connection
              willCacheResponse:(NSCachedURLResponse*)cachedResponse {
    return nil;
}
- (void)connectionDidFinishLoading:(FileURLConnection *)connection {
    NSLog(@"connectionDidFinishLoading run");
    NSMutableData *dataForConnection = [self dataForConnection:(FileURLConnection *)connection];
    NSString *fileName = connection.tag;
    NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]stringByAppendingPathComponent:fileName];
    [[NSFileManager defaultManager] createFileAtPath:filePath contents:nil attributes:nil];
    file = [NSFileHandle fileHandleForUpdatingAtPath:filePath];
    [file seekToEndOfFile];
    [file writeData:dataForConnection];
    [file closeFile];
}

编辑:如果我在 getFile 中对 receivedData (NSDictionary) 进行 NSLog 更新,它会更新“标签”对象,但没有数据与之相关:

"accomodation.txt" = <>;
"future-congress-dates.txt" = <>;
"general-file.txt" = <>;
...

我也尝试过为每个代表记录 dataForConnection (NSMutableData),但没有打印任何内容:

NSLog(@"dataForConnection %@", dataForConnection);

最佳答案

好吧,我投降。 有完整代码:

文件网址连接:

@interface FileURLConnection: NSURLConnection

@property (nonatomic, strong) NSFileHandle *file;

@end

获取文件函数:

-(void)getFile {
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:fullURL]];
    FileURLConnection *conn = [[FileURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
}
- (void)connection:(FileURLConnection*)connection didReceiveResponse:(NSURLResponse *)response {
    NSString *fileName = [[response URL] lastPathComponent];
    NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]stringByAppendingPathComponent:fileName];
    [[NSFileManager defaultManager] createFileAtPath:filePath contents:nil attributes:nil];
    connection.file = [NSFileHandle fileHandleForUpdatingAtPath:filePath];
}
- (void)connection:(FileURLConnection *)connection didReceiveData:(NSData *)data {
    [connection.file writeData:data]; 
}
- (NSCachedURLResponse *)connection:(FileURLConnection *)connection
              willCacheResponse:(NSCachedURLResponse*)cachedResponse {
    return nil;
}
- (void)connectionDidFinishLoading:(FileURLConnection *)connection {
    NSLog(@"Connection is %@", connection);
    [connection.file closeFile];
}
- (void)connection:(FileURLConnection *)connection didFailWithError:(NSError *)error {
    NSLog(@"error - %@", error);
}

原始问题:App crashing when downloading a large file - NSFileHandle seekToEndOfFile

关于ios - 应用程序不使用 NSURLConnection 下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22008702/

相关文章:

iphone - 如何隐藏显示在 UITableView 中的空 UITableViewCells 以普通样式 - IOS?

ios - –removeInitialMenu ...属性上的方法

ios - 如何复制从iOS air应用程序加载的外部SWF中包含的AS3中的MovieClip?

ios - 如何在 IOS 8 中使用自定义键盘进行文字转语音?

ios - GIDSignInButton 为空

iphone - 如何将为iPhone创建的项目更改为ipad(通用)?

ios - 如何在自定义键盘中播放键盘点击声音?

ios - UIScrollView - 告诉 setContentOffset 和手动滚动之间的区别

objective-c - 我有一个循环引用。如何在 Objective-C 中创建弱引用?

ios - 如何在CoreData中正确添加大数据集?