Reputation: 15639
I want to copy the xml file from server to save it to locally, because if I will send request to server again and again, it will take time, so I want to copy the xml to local resources whenever app starts, then parse the local xml,
how can I do it?
Upvotes: 1
Views: 318
Reputation: 7900
First of all you need to download the file:
NSURL *url = [NSURL URLWithString:FILEURL];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[NSURLConnection connectionWithRequest:request delegate:self];
then add in the h file:
NSMutableData *receivedData;
and in the m file:
-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
if (receivedData)
{
[receivedData appendData:data];
}
else
{
receivedData = [[NSMutableData alloc] initWithData:data];
}
}
-(void) connectionDidFinishLoading:(NSURLConnection *)connection
{
//saving your data in the local
NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *fullName = [NSString stringWithFormat:@"xmlfile.xml"];
NSString *fullFilePath = [NSString stringWithFormat:@"%@/%@",docDir,fullName];
[receivedData writeToFile:fullFilePath atomically:YES];
}
edit: get the file from local-
NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *fullName = [NSString stringWithFormat:@"xmlfile.xml"];
NSString *fullFilePath = [NSString stringWithFormat:@"%@/%@",docDir,fullName];
NSData *myData = [NSData dataWithContentsOfFile:filePath];
now you can take the NSData when parse it,there are a lot of examples in the site.
Upvotes: 1