I have one 3 arrays that i am trying to save to 3 different plists in one view and then load that data into an array in another view. I am having trouble figuring out how to access the data in the plist in my other view and save it correctly in my initial view. Here is the code from my first view.
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [documentPaths objectAtIndex:0];
NSString *documentsDirectory1 = [documentPaths objectAtIndex:1];
NSString *documentsDirectory2 = [documentPaths objectAtIndex:2];
NSString *documentsDirectory3 = [documentPaths objectAtIndex:3];
NSString *documentPlistPath1 = [documentsDirectory stringByAppendingPathComponent:#"balance.plist"];
NSString *documentsPlistPath2 = [documentsDirectory1 stringByAppendingPathComponent:#"principal.plist"];
NSString *documentsPlistPath3 = [documentsDirectory2 stringByAppendingPathComponent:#"interest.plist"];
NSString *documentsPlistpath4 = [documentsDirectory3 stringByAppendingPathComponent:#"dates.plist"];
[dateValues writeToFile:documentsPlistpath4 atomically:YES];
[balanceLabels writeToFile:documentPlistPath1 atomically:YES];
[pricipalLabels writeToFile:documentsPlistPath2 atomically:YES];
Any help is greatly appreciated!
Thanks!
create an array
NSArray *array = #[#"abc", #"cba"];
get path to plist file
NSString *documentsFolderPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *fileName = #"my_plist_file";
NSString *filePath = [documentsFolderPath stringByAppendingPathComponent:fileName];
save an array to .plist
[array writeToFile:filePath atomically:YES];
read an array from .plist
NSArray *arrayFromDisk = [NSArray arrayWithContentsOfFile:filePath];
Related
How can I store an image and any string value in plist as dictionary.I
have created a plist file in the project,and
also how can i append the file with same dictionary of different image and name?
Use the following code to write image & title to-gather in plist-
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"PlistName.plist"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath: path])
{
path = [documentsDirectory stringByAppendingPathComponent: [NSString stringWithFormat: #"PlistName.plist"] ];
}
fileManager = [NSFileManager defaultManager];
NSMutableDictionary *dataDictionary=[NSMutableDictionary alloc] init];
[dataDictionary setObject:ImageData forKey:image];
[dataDictionary setObject:ImageTitle forKey:title];
[dataDictionary writeToFile: path atomically:YES];
And read your image & title dictionary from plist like this-
- (NSDictionary * )readDataFromPlist()
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"PlistName.plist"];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSMutableDictionary *dataDictionary;
if ([fileManager fileExistsAtPath: path])
{
dataDictionary = [[NSMutableDictionary alloc] initWithContentsOfFile: path];
return dataDictionary;
}else
return nil;
}
You can follow this link. It provides detail how to store image to plist
Storing image in plist
And for title to store you can add new key like below.
// Get a full path to a plist within the Documents folder for the app
NSString *titleStr = #"Title Of Image";
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask,
YES);
NSString *path = [NSString stringWithFormat:#"%#/YOURPLIST.plist",
[paths objectAtIndex:0]];
// Place an image in a dictionary that will be stored as a plist
[dictionary setObject:image forKey:#"image"];
[dictionary setObject:titleStr forKey:#"title"];
// Write the dictionary to the filesystem as a plist
[NSKeyedArchiver archiveRootObject:dictionary toFile:path];
I have 2 methods, one for saving an array to a file and one for loading an array from the file, but when I'm trying to read an array, nothing happens, like an array is empty.
My code:
- (IBAction)write:(id)sender
{
NSArray *array = [racersArray copy];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *libraryDirectory = [paths objectAtIndex:0];
NSString *location = [libraryDirectory stringByAppendingString:#"/history.plist"];
[array writeToFile:location atomically:YES];
}
- (IBAction)read:(id)sender
{
NSString *path = [[NSBundle mainBundle] pathForResource:#"file" ofType:#"plist"];
NSArray *array = (path != nil ? [NSArray arrayWithContentsOfFile:#"/history.plist"] : nil);
}
You are reading/writing from/to two different places.
You write to the Library folder (it should be under Library/Application Support or some such, but I digress).
You read from the app bundle.
Try with this Methods may be helps you.👍
For Save
-(IBAction)save_Action:(id)sender{
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *plistPath = [documentsPath stringByAppendingPathComponent:#"manuallyData.plist"];
[plistData writeToFile:plistPath atomically:YES];
}
For Get Path
-(IBAction)retrive:(id)sender{
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *plistPath = [documentsPath stringByAppendingPathComponent:#"manuallyData.plist"];
if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath])
{
plistPath = [[NSBundle mainBundle] pathForResource:#"manuallyData" ofType:#"plist"];
}
}
- (IBAction)savetapped:(id)sender {
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *plistPath = [documentsPath stringByAppendingPathComponent:#"json.geojson"];
[paths writeToFile:plistPath atomically:YES];
}
- (IBAction)retriveTapped:(id)sender {
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0]; NSString *plistPath = [documentsPath stringByAppendingPathComponent:#"json.geojson"];
NSLog(#"%#",documentsPath);
if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath])
{
plistPath = [[NSBundle mainBundle] pathForResource:#"json" ofType:#"geojson"];
}
NSLog(#"%#",plistPath );
}
Took a while to figure out that I don't actually know what you are trying to do.
pathForResource gives you paths for resource in your application bundle. You can only read from there. You can never write there. Sometimes people have the initial version of a file in the application bundle and then write it somewhere else. If that's what you want then most likely you need to:
(1) write a method that returns the path where the array will be stored once your application has been running.
(2) The "write" method calls this path method to know where to write the array.
(3) The "read" method calls this path method to know where to read the array, then you read it. However, the array can be nil because your app was run the first time, or the array wasn't written correctly. In that case, you use pathForResource to get the path where the file would be in your application bundle, and read it from there.
I am trying to do something relatively simple. I have a .plist in my bundle and I am trying to save it to the documents directory with encryption. Now before I tried added encryption, it worked fine. However a new crash has arose.
This is how I save my .plist:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"Hi.plist"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath: path]) {
NSString *bundle = [[NSBundle mainBundle] pathForResource:#"GameSave" ofType:#"plist"];
NSData *plistData = [NSData dataWithContentsOfFile:bundle];
[NSKeyedArchiver archiveRootObject:plistData toFile:path];
plistData = [plistData AES256EncryptWithKey:#"536335"];
[plistData writeToFile:path atomically:YES];
}
Then this is how I retrieve my .plist (and later change a value and re-save)
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"Hi.plist"];
//Get array and then current level dict
NSData *plistData = [[NSData alloc] initWithContentsOfFile:path];
plistData = [plistData AES256DecryptWithKey:#"1111"];
NSMutableArray *savegameArray = [[NSKeyedUnarchiver unarchiveObjectWithData:plistData] mutableCopy];
int objectIndex = [Singleton sharedInstance].levelNumber - 1;
NSMutableDictionary *levelDict = [[savegameArray objectAtIndex:objectIndex] mutableCopy];
[levelDict setObject:videoID forKey:#"RecordingURL"];
//Now put new dict back in array
[savegameArray replaceObjectAtIndex:objectIndex withObject:levelDict];
NSData *savedarrayData = [NSKeyedArchiver archivedDataWithRootObject:savegameArray];
savedarrayData = [savedarrayData AES256EncryptWithKey:#"1111"];
[savedarrayData writeToFile:path atomically:YES];
However, in the read code every time I get to this line: NSMutableArray *savegameArray = [[NSKeyedUnarchiver unarchiveObjectWithData:plistData] mutableCopy]; There is a SIGABRT crash which prints:
'-[__NSCFArray objectForKey:]: unrecognized selector sent to instance
0x16604140'
What am I doing wrong?
As we discovered through our chat, we had to first convert the decrypted NSData object to a proper NSPropertyListSerialization serialization.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"Hi.plist"];
NSData *plistData = [[NSData alloc] initWithContentsOfFile:path];
NSData *newData = [plistData AES256DecryptWithKey:#"1111"];
NSPropertyListFormat format;
NSMutableArray *savegameArray = [[NSPropertyListSerialization propertyListFromData:newData mutabilityOption:NSPropertyListMutableContainersAndLeaves format:&format errorDescription:nil]mutableCopy];
NSLog(#"Array: %#",savegameArray);
You just need to allocate memory to your NSMutableArray like...
NSMutableArray *arrayName = [[NSMutableArray alloc] init];
Until you do that crash will arose..
Hope this is helpful.
Thanks,
Rajesh..
I have text file text.txt in my xcode project and I need to write NSString init from my iOS app. I've tried this code
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDir = [paths objectAtIndex: 0];
NSString *docFile = [docDir stringByAppendingPathComponent: #"text.txt"];
NSString *content = #"Test";
[content writeToFile: docFile atomically: NO];
But it doesn't work for me... So can you help me with it?
this worked for me
//Method writes a string to a text file
-(void) writeToTextFile{
//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 *fileName = [NSString stringWithFormat:#"%#/text.txt",
documentsDirectory];
//create content - four lines of text
NSString *content = #"Test";
//save content to the documents directory
[content writeToFile:fileName
atomically:NO
encoding:NSStringEncodingConversionAllowLossy
error:nil];
}
How do I retrieve image data from plist and display to that data to an image view?
- (void)getImages {
// 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];
NSArray *imageArray = [plist objectForKey:#"imageArray"];
for (NSString *imageString in imageArray) {
DLog(#"Image Name: %#", imageString);
}
}
Get plist code from www.iphoneexamples.com