can't creating plist file in document folder - iphone

I have write code like this in Viewdidload method. But it don't create file .plist.
I don't know whether code is wi8 or wrong. And where i have to write?
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; //2
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"data1.plist"]; //3
NSFileManager *fileManager = [NSFileManager defaultManager];
if ( ![fileManager fileExistsAtPath:path] ) {
NSString *pathToSettingsInBundle = [[NSBundle mainBundle]
pathForResource:#"data1" ofType:#"plist"];
}

You have just created a path to it, you should try loading it into a NSDictionary.
NSDictionary* mydictionary = [ NSDictionary dictionaryWithContentsOfFile: pathToSettingsInBundle];

Inside your if block add a second line
[fileManager copyItemAtPath:pathToSettingsInBundle toPath:path error:&error];

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
plistpath = [[NSString alloc] initWithString:[documentsDirectory stringByAppendingPathComponent:#"Product.plist"]];
And then
[array writeToFile:path atomically:YES];
[Whatever [Array or Dictionary] you want to Write in Plist]
If it is still not creating the plist then check whether your array or dictionary must be empty.

Related

Increment Number in plist

I am using the method below to get an array from my plist and then increase a certain value by 1, then save it. However I log the array and the value doesn't actually go up each time.
In my plist, I have an array and in that number values, each one is set to 0. So every time I run this again it goes back to 0 it seems.
NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *finalPath = [path stringByAppendingPathComponent:#"Words.plist"];
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithContentsOfFile:finalPath];
NSMutableArray *errors = [dict objectForKey:[NSString stringWithFormat:#"Errors%d.%d", [[stageSelectionTable indexPathForSelectedRow] section] +1, [[stageSelectionTable indexPathForSelectedRow] row] +1]];
int a = [[errors objectAtIndex:wordIndexPath] intValue];
a += 1;
NSNumber *b = [NSNumber numberWithInt:a];
[errors replaceObjectAtIndex:wordIndexPath withObject:b];
[errors writeToFile:finalPath atomically:YES];
You can only write to a file in the documents-folder. You can't write to your bundle!
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:#"Namelist.plist"];
You can use NSFilemanager to copy your Plist-File to the documents-folder.
To get the path of your file:
- (NSString *)filePath {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:#"MyFile.plist"];
return filePath;
}
To copy the file if it doesn't exist:
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:[self filePath]]) {
NSString *path = [[NSBundle mainBundle] pathForResource:#"MyFile" ofType:#"plist"];
[fileManager copyItemAtPath:path toPath:[self filePath] error:nil];
}
Now you can write your NSDictionary to the Documents-Directory:
[dict writeToFile:[self filePath] atomically:YES];
But you really need to update the array in the dict!
You are writing the array to disk, instead of the dictionary that the array originated from:
[dict writeToFile:finalPath atomically:YES];
Also, you will need to replace the Errors%d.%d object with the updated one before saving it:
[dict setObject:errors forKey:/* your formatted key*/];
Finally, as #mavrick3 pointed out, you cannot save files to your bundle, only to your application's documents directory.

How to load PDFs from Documents Directory?

I can load list of pdf files(all pdf files) from Resources folder, using this code.
NSArray *pdfs = [[NSBundle mainBundle] pathsForResourcesOfType:#"pdf" inDirectory:nil];
But I couldn't find a method to load list of pdf files from Documents Directory. The following line of code can only load one pdf at a time.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *file = [documentsDirectory stringByAppendingPathComponent:#"test.pdf"];
NSURL *urlPdf = [NSURL fileURLWithPath: file];
How could I load all pdf files from Documents Directory
Can you please give me a code example.
I was able to solve my issue using this line of code :)
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSArray *pdfs = [[NSBundle bundleWithPath:[paths objectAtIndex:0]] pathsForResourcesOfType:#"pdf" inDirectory:nil];
Go with NSFileManager's contentsOfDirectoryAtPath and filter the result by hand for pdf files.
This answer maybe helpful.
You could do this
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error = nil;
NSArray *filesAtPath = [fileManager contentsOfDirectoryAtPath:documentsDirectory error:error];
for (NSString *path in filesAtPath)
{
if ([path rangeOfString:#".pdf"].length > 0)
{
//Do whatever you want with the pdf file
}
}
I hope it helps, cheers

How can I get all the file names of documents in my sandbox?

I have the following code:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
However I am clueless as to how to get all the files names and assign them to an array?
Any help would be appreciated.
NSFileManager has a method called contentsOfDirectoryAtPath:error: that returns an array of all the files in that directory.
You can use it like this:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
if([paths count] > 0)
{
NSString *documentsDirectory = [paths objectAtIndex:0];
NSError *error = nil;
NSArray *documentArray = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory error:&error];
if(error)
{
NSLog(#"Could not get list of documents in directory, error = %#",error);
}
}
The documentsArray object will contain a list of all the files in that directory.
Use NSFileManager's contentsOfDirectoryAtPath:error: method.
NSError *error;
NSFileManager* fileMgr = [NSFileManager defaultManager];
NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:#"Documents"];
NSArray* documentsArray = [fileMgr contentsOfDirectoryAtPath:documentsDirectory error:&error];
[documentsArray count];
Is another way of doing this for those with a different set up.

NSMutableDictionary

How to use NSMutable Dictionary in another class for read and write data from plist file in iphone....
Any Ideas?
As you can find in in NSDictionary reference, this class has a method to create a nsdictionary from a file initWithContentsOfFile:(NSString *)path . You can do something like:
NSString *plistPath = [[NSBundle mainBundle] pathForResource:#"myDictionary" ofType:#"plist"];
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
Where myDictionary is your plist name (In case of your plist is inside resource bundle).
You can write a plist on disk with the method:
[dict writeToFile:filePath atomically: YES];
Where filePath is your destination path.
See the Plist files example on this tutorial.
// Look in Documents for an existing plist file
NSArray *paths = NSSearchPathForDirectoriesInDomains(
NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
myPlistPath = [documentsDirectory stringByAppendingPathComponent:
[NSString stringWithFormat: #"%#.plist", plistName] ];
[myPlistPath retain];
// If it's not there, copy it from the bundle
NSFileManager *fileManger = [NSFileManager defaultManager];
if ( ![fileManger fileExistsAtPath:myPlistPath] ) {
NSString *pathToSettingsInBundle = [[NSBundle mainBundle]
pathForResource:plistName ofType:#"plist"];
}
NSArray *paths = NSSearchPathForDirectoriesInDomains(
NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectoryPath = [paths objectAtIndex:0];
NSString *path = [documentsDirectoryPath
stringByAppendingPathComponent:#"myApp.plist"];
NSMutableDictionary *plist = [NSDictionary dictionaryWithContentsOfFile: path];
myKey = (int)[[plist valueForKey:#"myKey"] intValue];
myKey2 = (bool)[[plist valueForKey:#"myKey2"] boolValue];
[plist setValue:myKey forKey:#"myKey"];
[plist writeToFile:path atomically:YES];

How to read a file in iPhone?

I am trying to read a .txt file from my documents directory in the form of NSString. Any idea how to read the file into NSString?
Thank you...
Use NSString's stringWithContentsOfFile: method.
NSString *fileContents = [NSString stringWithContentsOfFile:#"some/file.txt"];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *myPathDocs = [documentsDirectory stringByAppendingPathComponent:#"myfile.txt"];
if (![[NSFileManager defaultManager] fileExistsAtPath:myPathDocs])
{
NSString *myPathInfo = [[NSBundle mainBundle] pathForResource:#"myfile" ofType:#"txt"];
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager copyItemAtPath:myPathInfo toPath:myPathDocs error:NULL];
}
//Load from File
NSString *myString = [[NSString alloc] initWithContentsOfFile:myPathDocs encoding:NSUTF8StringEncoding error:NULL];
This worked for me
Anyway, thank you all..
Hopefully this is what you're after :
NSString *myString = [[NSString alloc] initWithContentsOfFile:#"pathToFile"];
I usually have it looking in the Applications Document directory.
Hope this helps...
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fileContents=[documentsDirectory stringByAppendingPathComponent:#"file.txt"];
hAPPY cODING...
I think this is the best way to read .txt file from DocumentDirectory.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:#"txtFile.txt"];
NSString *content = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:NULL];
I hope this will work for you!!