I’m building an application that parses XML from an online server. I want to cache the XML file locally for 24 hours. How can I change the following code to read from a file in my resource directory?
parser = [[NSXMLParser alloc] initWithContentsOfURL: [NSURL URLWithString:url]];
You can obtain the path for your xml file like the following:
and then obtain a NSData instance like the following:
and finally call
initWithDatamethod ofNSXMLParser.But you cannot modify the content of your application bundle. Files in the bundle (the resources directory you asked for) cannot be modified.
There are different way to do it:
NSFileManagerclass to save and read your xml fileAn example with
NSFileManager:The snippet grab the xml file path contained in your Documents directory. It means that there is already a file there. For further info see NSFileManager class and how-to-save-your-app-data-with-nscoding-and-nsfilemanager
Hope it helps.