Reading a file from documents directory Objective-c - objective-c

I write a file to the documents directory of an app:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
// the path to write file
NSString *file = [documentsDirectory stringByAppendingPathComponent:#"data.json"];
[responseString writeToFile:file atomically:YES];
But how can I access the file?
My code looks like this:
NSString *filePath = [[NSFileManager defaultManager] pathForRessorce: #"data" ofType:#"JSON"];
NSString *fileContent = [[NSString alloc] initWithContentsOfFile:filePath];
SBJsonParser *parser = [[SBJsonParser alloc] init];
NSDictionary *data = (NSDictionary *) [parser objectWithString:fileContent error:nil];
The file path is not populated...can somebody give me a hint?
Thanks!

You are reading from resource folder, not from document directory. You need to populate filePath in the same way that you have used during writing. That is:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:#"data.json"];
NSString *fileContent = [[NSString alloc] initWithContentsOfFile:filePath];

"initwithcontentsoffile is deprecated" warning
So Instead of:
NSString *fileContent = [[NSString alloc] initWithContentsOfFile:filePath];
use:
NSString *userInfoFileContent = [[NSString alloc] initWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&error];

Related

Plist/Document Directory is null when reading plist

I just want to replace static plist data to dynamic.this is my code
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *docDirPath = [documentsDirectory stringByAppendingPathComponent:#"sample.plist"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if(![fileManager fileExistsAtPath: docDirPath])
{
NSString *bundle = [[NSBundle mainBundle] pathForResource:#"sample" ofType:#"plist"];
[fileManager copyItemAtPath:bundle toPath:docDirPath error:&error];
NSLog(#"plist is copied to Directory");
}
i have some data in event array but its not writing/reading can you please suggest me where i am wrong.
NSLog(#"data coming from server....%#",self.eventDataArray);
//Writing array to document directory
NSArray *docpaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectoryPath = [docpaths objectAtIndex:0];
NSString *path = [NSString stringWithFormat:#"%#/sample.plist", documentsDirectoryPath];
[self.eventDataArray writeToFile:path atomically:YES];
NSLog(#"plist path string....%#",path);
//reading from document directory
NSMutableArray *readingArray=[[NSMutableArray alloc] initWithContentsOfFile:path];
NSLog(#"plist reading data is here......%#",readingArray);

How to Save data into Project Folder?

I download a XML file from web and save it in a folder.With these Codes
NSURL *goo = [[NSURL alloc]initWithString:#"......xml"];
NSData *data = [[NSData alloc] initWithContentsOfURL:goo];
NSString *xml = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSString *documentsDirectory = #"/Users/MacBook/Desktop/Bebegim_Icin_Yarisiyorum/Milyoner/Class";
NSString *xmlFilePath = [documentsDirectory stringByAppendingPathComponent:#"qpack1.xml"];
[xml writeToFile:xmlFilePath atomically:YES encoding:NSUTF8StringEncoding error:nil];
Class Folder is in myProject Folders.
But Apple does not have this path.I need to say save the
xml file in Class folder of My project Folders.
/Users/MacBook/Desktop/Bebegim_Icin_Yarisiyorum/Milyoner/Class
Can anyone help me,I should say ../Class or .Class or ./Class?
What should I say when I am creating documentDirectory.
Try:
NSString *documentsDirectory = [NSHomeDirectory()stringByAppendingPathComponent:#"Documents/qpack2.xml"];
Or:
NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)
NSString *docsPAth = [pathArray objectAtIndex:0];
NSString *documentsDirectory = [documentsDirectory stringByAppendingPathComponent:#"qpack2.xml"];

Saving NSMutableDictionary to plist iOS

This has become very frustrating over the past day-and-a-bit-too-much: why doesn't this work?
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%#.plist",kAllScoresUnorderedArray]];
NSMutableDictionary *test = [[NSMutableDictionary alloc] init];
[test setObject:#"bob" forKey:#"tmo"];
[test writeToFile:path atomically: YES];
I have used it before, and it worked perfectly: is there something I am forgetting to add in my info.plist? That is the only thing I can think of.
Thank you for helping!
EDIT:
Maybe it has something to do with Xcode 4.5 and iOS6? It worked in 4.3/iOS5
Have you added the .plist file to your project? If so, try this:
NSError *error;
NSFileManager *fileManager = [[NSFileManager alloc] init];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%#.plist",kAllScoresUnorderedArray]];
if (![fileManager fileExistsAtPath: path])
{
NSString *bundle = [[NSBundle mainBundle] pathForResource:kAllScoresUnorderedArray ofType:#"plist"];
[fileManager copyItemAtPath:bundle toPath: path error:&error];
}
NSMutableDictionary *test = [[NSMutableDictionary alloc] initWithContentsOfFile: path];
[test setObject:#"bob" forKey:#"tmo"];
[test writeToFile:path atomically: YES];
If not, create the property list in your project.

Saving dropbox file in path

This gives me...
NSString *filename2 = [NSString stringWithFormat:#"/newFile.json"];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *getImagePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%#", filename2]];
[self.restClient loadFile:filename2 intoPath:getImagePath];
NSString *fileContent = [[NSString alloc] initWithContentsOfFile:getImagePath];
SBJsonParser *parser = [[SBJsonParser alloc] init];
NSDictionary *data = (NSDictionary *) [parser objectWithString:fileContent error:nil];
...this error:
File loaded into path: (null)
...but why?? this code was under view did load. the file is in dropbox, so what is the problem

I have successfully saved my plist to the documents directory! Now I can't load it back

This code works beautifully to save my plist to the documents directory.
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.rightBarButtonItem = self.addButtonItem;
NSString *path = [[NSBundle mainBundle] pathForResource:#"TitleArray" ofType:#"plist"];
NSMutableArray *tmpArray = [[NSMutableArray alloc]initWithContentsOfFile:path];
self.titles = tmpArray;
[tmpArray release];
//TESTING NEW CODE FOR SAVING TO DOC DIR
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *docDirPath = [documentsDirectory stringByAppendingPathComponent:#"TitleArray.plist"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if(![fileManager fileExistsAtPath: docDirPath])
{
NSString *bundle = [[NSBundle mainBundle] pathForResource:#"TitleArray" ofType:#"plist"];
[fileManager copyItemAtPath:bundle toPath:docDirPath error:&error];
NSLog(#"plist is copied to Directory");
}
I have not been able to figure out how to load the plist back to the app!
Can anyone help? Thanks.
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"TitleArray.plist"];
NSArray * myList = [NSArray arrayWithContentsOfFile:path];
NSMutableArray *tmpArray = [myList mutableCopy];
self.titles = tmpArray;
[tmpArray release];
didn't work?
If you are asking how to overwrite a plist included in your app's main bundle, then you should know that it cannot be done. Take a look at this question for more details: Can you update a file in the application bundle?
NSError *error; NSArray *paths =
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *docDirPath = [documentsDirectory stringByAppendingPathComponent:#"TitleArray.plist"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if(![fileManager fileExistsAtPath: docDirPath])
{
docDirPath = [[NSBundle mainBundle] pathForResource:#"TitleArray" ofType:#"plist"];
}
NSMutableArray *tmpArray = [[[NSMutableArray alloc] initWithContentsOfFile: docDirPath] mutableCopy];