简体   繁体   English

目标C接收数据并将其另存为xml文件,以在不连接互联网的情况下进行读取

[英]Objective C Receive data and save it as xml file for read it without an internet connection

I have this method to get xml file from google reader with the contens of the feeds reader. 我有这种方法与提要阅读器的内容从谷歌阅读器获取xml文件。 I want to save locally the data (I'm sure that data is an xml file) how can I do??? 我想在本地保存数据(我确定数据是xml文件),我该怎么办? I can store locally data to read it later without internet connection? 我可以存储本地数据以便以后在没有互联网连接的情况下阅读? HOw can I do? 我能怎么做? thank 谢谢

- (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];
}

EDIT ** 编辑**

I USE THIS CODE BUT DOESN'T WORK 我使用此代码却不起作用

- (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];
}

You can use writeToFile:atomically: method to store your NSData object. 您可以使用writeToFile:atomically:方法存储您的NSData对象。
Sample: 样品:

[data writeToFile:path atomically:YES];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM