我有这种方法从谷歌阅读器获取xml文件与feed阅读器的竞争。 我想在本地保存数据(我确定数据是一个xml文件)我该怎么办? 我可以存储本地数据以便以后读取它而无需连接互联网?我能怎么做?感谢
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)aResponse
{
NSLog(@"------------------------------- connectionDidReceiveResponse");
expectedResponseLength = [NSNumber numberWithFloat:[aResponse expectedContentLength]];
URLresponse = aResponse;
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
NSLog(@"------------------------------- connectionDidReceiveData: %@", data);
//float l = [responseData length];
//[delegate GoogleReaderRequestReceiveBytes:l onTotal:[expectedResponseLength floatValue]];
//TESTING
NSString *string = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
NSLog(@"the data %@",string);
[self.responseData appendData:data];
}
编辑**
我使用此代码但不工作
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)aResponse
{
NSLog(@"------------------------------- connectionDidReceiveResponse");
expectedResponseLength = [NSNumber numberWithFloat:[aResponse expectedContentLength]];
URLresponse = aResponse;
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
NSLog(@"------------------------------- connectionDidReceiveData: %@", data);
//float l = [responseData length];
//[delegate GoogleReaderRequestReceiveBytes:l onTotal:[expectedResponseLength floatValue]];
//TESTING
NSString *string = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
NSLog(@"the data %@",string);
NSString *path=@"Library/News";
[data writeToFile:path atomically:YES];
[self.responseData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didSendBodyData:(NSInteger)bytesWritten totalBytesWritten:(NSInteger)totalBytesWritten totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite;
{
NSLog(@"------------------------------- connectionDidSendBodyData: %d", totalBytesWritten);
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)theError
{
NSLog(@"------------------------------- connectionDidFailWithError: %@", [theError localizedDescription]);
self.responseData = nil;
NSString *path=@"Library/News";
NSData *data = [[NSData alloc] initWithContentsOfFile:path];
[self.responseData appendData:data];
[delegate GoogleReaderRequestDidFailWithError:theError];
}
答案 0 :(得分:5)
您可以使用writeToFile:atomically:
方法存储NSData对象。
样品:
[data writeToFile:path atomically:YES];