2011-08-06 42 views
0

我有這種方法可以從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]; 
} 
+2

ü[R使用錯誤的道路。你應該使用應用程序的文檔目錄。 'NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES); NSString * pathToDocuments = [paths objectAtIndex:0];'。 – VenoMKO

+0

並保存數據? NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES); NSString * pathToDocuments = [paths objectAtIndex:0]; [data writeToFile:atomToDocuments:YES]; 它是否正確? –

+0

是的。並且不要忘記使用'stringByAppendingPathComponent:'方法添加文件名。 – VenoMKO

回答

5

您可以使用writeToFile:atomically:方法來存儲您的NSData對象。
樣品:

[data writeToFile:path atomically:YES]; 
+0

謝謝,我怎樣才能讀取這些數據?例如,我的數據是一個XML文件,我怎麼讀它? –

+1

Ypu歡迎。 'NSData * data = [[NSData alloc] initWithContentsOfFile:path];' – VenoMKO

相關問題