使用NSURLConnection时如何制作完整的数据?
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
}
粘贴NSData的部分? 我应该做什么,我想从url地址下载文件到我的文档目录,我想知道已经下载了多少字节来完成最好的方法?
答案 0 :(得分:2)
创建NSMutableData并在收到数据时附加它。
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
if (responseData == nil) {
responseData = [[NSMutableData data] retain];
}
[responseData appendData:data];
}
或者
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
responseData = [[NSMutableData data] retain];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[responseData appendData:data];
}
然后在connectionDidFinishLoading
请务必在完成后释放数据和连接。
有关详细信息,请参阅此文档:https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLConnection.html