我有這種方法可以從google閱讀器獲取xml文件,並與feeds閱讀器進行比較。 我想本地保存數據(我確信數據是一個xml文件)我該怎麼辦? 我可以存儲本地數據,以後再閱讀它,而無需連接互聯網?我能怎麼做?感謝Objective C接收數據並將其保存爲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 *[email protected]"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 *[email protected]"Library/News";
NSData *data = [[NSData alloc] initWithContentsOfFile:path];
[self.responseData appendData:data];
[delegate GoogleReaderRequestDidFailWithError:theError];
}
ü[R使用錯誤的道路。你應該使用應用程序的文檔目錄。 'NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES); NSString * pathToDocuments = [paths objectAtIndex:0];'。 – VenoMKO
並保存數據? NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES); NSString * pathToDocuments = [paths objectAtIndex:0]; [data writeToFile:atomToDocuments:YES]; 它是否正確? –
是的。並且不要忘記使用'stringByAppendingPathComponent:'方法添加文件名。 – VenoMKO