Writing to plists located at resource folder in iPhone - iphone

Any one please answer me, how to write into the plist file which is located in my resource folder. Please write an example code for me.

You can't. The application bundle is not writable on iPhone OS devices.

-(IBAction)textFieldCheck:(id) sender {
myPrimaryinfo = [[NSMutableArray arrayWithCapacity:6]retain];
NSArray *keys = [NSArray arrayWithObjects:#"Date",#"Time",#"Address",#"City",#"State",#"Zipcode",nil];
[myPrimaryinfo addObject:[NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:
[NSString stringWithFormat:#"%#",txtDate.text],
[NSString stringWithFormat:#"%#",txtTime.text],
[NSString stringWithFormat:#"%#",txtAddress.text],
[NSString stringWithFormat:#"%#",txtCity.text],
[NSString stringWithFormat:#"%#",txtState.text],
[NSString stringWithFormat:#"%#",txtZip.text],nil]forKeys:keys]];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"myPrimaryinfo.plist"];
[myPrimaryinfo writeToFile:path atomically:NO];
NSLog(#"PrimaryInfo array: %#", myPrimaryinfo);
}

Related

How to select all the images from my iPhone App document directory

I have a folder containing a bunch of images. I would like to add the names of all the images to an array. The images are in a folder in my document on the app. See screen shot.
I know If I would like to get one of these images I would use the following code.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:#"%#/POIs/%#", documentsDirectory,image];
Is it posible to select all the files in the folder? If it is would it also be possible to select the names of the files and add them to an array?
Thanks
Get the file path of the POS folder:
NSString *filePath = [NSString stringWithFormat:#"%#/POIs", documentsDirectory];
and then do:
NSArray *files = [fileManager contentsOfDirectoryAtPath:filePath
error:nil];
code not tested
Try with below:
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
library = [path objectAtIndex:0];
fileArray = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:library error:nil];
NSLog(#"fileArray...%#",fileArray);
for(int i = 0;i<fileArray.count;i++)
{
id arrayElement = [fileArray objectAtIndex:i];
if ([arrayElement rangeOfString:#".png"].location !=NSNotFound)
{
[imagelist addObject:arrayElement];
arrayToLoad = [[NSMutableArray alloc]initWithArray:imagelist copyItems:TRUE];
}
}
You can do like this :
NSArray *directoryContent = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject] error:NULL];
To set Image :
[imgView setImage:[UIImage imageWithContentsOfFile:"Your complete path"]];

Problem writing NSData in my device

Can someone explain me why my code doesn't work?
//get the documents directory:
NSArray *paths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
//make a file name to write the data to using the
//documents directory:
NSString *fullFileName = [NSString stringWithFormat:#"%#/subscriptions", documentsDirectory];
[dataReply writeToFile:fullFileName atomically:NO];
NSData *getData=[[NSData alloc]initWithContentsOfFile:fullFileName];
infact if I try
NSString *string = [[NSString alloc] initWithData:getData encoding:NSASCIIStringEncoding];
while if I load myData without save it to my device it works! (So I'm sure that my data is not empty)
Try This,
NSArray *paths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
//make a file name to write the data to using the
//documents directory:
NSString *fullFileName = [NSString stringWithFormat:#"%#/subscriptions", documentsDirectory];
[dataReply writeToFile:fullFileName atomically:NO];
Nsdata *getData=[fullFileName dataUsingEncoding:NSUTF8StringEncoding];

Not able to write to Plist

I have a very basic question about how to write to a plist. I added a "Here.plist" file to my resources folder. The following is my code:
NSString *path = [[NSBundle mainBundle]pathForResource:#"Here" ofType:#"plist"];
NSArray *array = [[NSArray alloc]initWithObjects:#"First", #"Second", #"Third", nil];
[array writeToFile:path atomically:YES];
"Here.plist" is still empty after executing the code. I even tried using a NSDictionary as follows:
NSDictionary *dict = [[NSDictionary alloc]initWithObjectsAndKeys:#"firstValue", #"First", #"secondValue", #"Second", #"thirdValue", #"Third", nil];
[dict writeToFile:path atomically:YES];
I went through many tutorials and forums but its just not working for me. What am i doing wrong? Losing my head over this.
You can't write into the bundle. Try writing into the documents directory. For example:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSArray *array = [[NSArray alloc]initWithObjects:#"First", #"Second", #"Third", nil];
[array writeToFile:[documentsDirectory stringByAppendingPathComponent:#"Here.plist" atomically:YES];
I suspect your problem is due to the fact that you can't write to the application bundle, only to your application's document store.
To obtain a path to the document store, use:
NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docStorePath = [searchPaths objectAtIndex:0];
NSString *filePath = [docStorePath stringByAppendingPathComponent:#"XXXXX"];

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];

NSMutableArray Problem - iPhone

I'm trying to get a UITableView to read it's data from a file. I've attempted it like this:
NSArray *paths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fullFileName = [NSString stringWithFormat:#"%#/entries.plist", documentsDirectory];
self.dataForTable = [[NSMutableArray alloc] initWithContentsOfFile:fullFileName];
This compiles fine, but when saving something to the file in the following snippet, the file is not saved nor anything is written to the array:
NSMutableDictionary*userDictionary;
userDictionary = [[NSMutableDictionary alloc] init];
[userDictionary setObject:name.text forKey:#"name"];
[userDictionary setObject:email.text forKey:#"email"];
[userDictionary setObject:serial.text forKey:#"serial"];
[userDictionary setObject:notes.text forKey:#"notes"];
[userDictionary setObject:[NSNumber numberWithInt:[licenseType selectedRowInComponent:0]] forKey:#"license_type"];
[userDictionary setObject:[date date] forKey:#"date"];
[userDictionary setObject:[NSNumber numberWithBool:[paymentSwitch isOn]] forKey:#"payment"];
NSString*dirToSaveTo = [NSHomeDirectory() stringByAppendingPathComponent:#"Documents"];
NSString*fileName = [NSString stringWithFormat:#"%#.plist",name.text];
NSString*saveName = [dirToSaveTo stringByAppendingPathComponent:fileName];
[userDictionary writeToFile:saveName atomically:NO];
NSArray *paths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fullFileName = [NSString stringWithFormat:#"%#/entries.plist", documentsDirectory];
[self.dataForTable addObject:name.text];
NSLog(#"%#",self.dataForTable);
[self.dataForTable writeToFile:fullFileName atomically:YES];
The NSLog just returns (null). The *plist file is never written. What am I doing wrong?
Almost certainly this line is failing, and you're not checking whether it returned nil:
self.dataForTable = [[NSMutableArray alloc] initWithContentsOfFile:fullFileName];
Once it does return nil, every other call to it does nothing. The most likely problems are that the file path you have constructed is incorrect and doesn't point to the file, or that the file is not a proper plist.