Please consider subscribing to Mobile Developer Tips RSS feed or following us on Twitter
|
|
|
Below are a few lines of code to get you started if you need to open and read a file that is stored in the application bundle. Files could be anything from help text that your application displays to specific content that facilitates testing.
As an example of the later, often times I find it helpful to put json (or xml) content into a file that I know is valid based on a remote server that I will eventually connect with. I add this file to the application bundle and read it into an internal data structure. I can then do various tests with known data, and once that is working, I can then proceed to the networking side of things to connect and retrieve live data.
// Read in and store as string
NSString *file = [[NSBundle mainBundle] pathForResource:@"jsonTest" ofType:@"txt"];
NSString *str = [NSString stringWithContentsOfFile:file
encoding:NSUTF8StringEncoding error:NULL];
debug(@"str: %@", str);
// Do something with the test data...
// Read in and store as raw data bytes
NSString *file2 = [[NSBundle mainBundle] pathForResource:@"data" ofType:@"txt"];
NSData *data = [NSData dataWithContentsOfFile:file2];
Related posts:
- Read and Write Array, Dictionary and Other Collections to Files
- Get Application Name
- iPhone File System: Creating, Renaming and Deleting Files
Leave Comment