paul_1991
paul_1991

Reputation: 245

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. I want to save locally the data (I'm sure that data is an xml file) how can I do??? 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];
}

Upvotes: 0

Views: 813

Answers (1)

VenoMKO
VenoMKO

Reputation: 3294

You can use writeToFile:atomically: method to store your NSData object.
Sample:

[data writeToFile:path atomically:YES];

Upvotes: 5

Related Questions