I want to write some values to an external file and use NSDictionary for this purpose:
NSString *s = #"123456";
NSString *key = #"key";
NSString *file = pathInAppDirectory(#"values");
NSDictionary *dic = [NSDictionary dictionaryWithObject:s forKey:key];
[dic writeToFile:file atomically:YES];
Ok, pathInAppDirectory looks like this:
NSString *pathInAppDirectory(NSString *fileName)
{
NSArray *documentDirectories = NSSearchPathForDirectoriesInDomains(NSApplicationDirectory, NSUserDomainMask, YES);
NSString *appDir = [documentDirectories objectAtIndex:0];
return [appDir stringByAppendingPathComponent:fileName];
}
What happens is exactly nothing: If I want to have look in the file somewhat later and want to display the values, null is returned. And a look into the folder of the iPhone simulator shows an empty folder, too. What happens there or, to be more specific: what does not happen?
You need to use NSDocumentDirectory instead of NSApplicationDirectory. It is not possible to write in the Application bundle.
Cleaned up example:
- (NSString *)pathInAppDirectory:(NSString *)fileName {
NSArray *documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *appDir = [documentDirectories objectAtIndex:0];
return [appDir stringByAppendingPathComponent:fileName];
}
NSString *s = #"123456";
NSString *key = #"key";
NSDictionary *dic = [NSDictionary dictionaryWithObject:s forKey:key];
NSString *filePath = [self pathInAppDirectory:#"values"];
[dic writeToFile:filePath atomically:YES];
Related
I am writing data to plist with always returns me the nil value.. I have to append data also to the previous values of array. But Values are always null..
Below is the code:
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
// get documents path
NSString *documentsPath = [paths objectAtIndex:0];
// get the path to our Data/plist file
NSString *plistPath = [documentsPath stringByAppendingPathComponent:#"a.plist"];
// set the variables to the values in the text fields
// create dictionary with values in UITextFields
NSDictionary *plistDict = [NSDictionary dictionaryWithObject:#"c" forKey:#"Id"];
if ([[NSFileManager defaultManager] fileExistsAtPath:plistPath])
{
array = [[NSMutableArray alloc] initWithContentsOfFile:plistPath];
[array addObject:plistDict];
[array writeToFile:plistPath atomically:YES];
NSLog(#"array%#",array);
}
else
{
NSArray *array = [NSArray arrayWithObject:plistPath];
[array writeToFile:plistPath atomically:YES];
}
If I understand your question correctly, I tested the code with just one little change i.e, Added array declaration and it's working fine. Please pardon me if this is not the issue.
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
// get documents path
NSString *documentsPath = [paths objectAtIndex:0];
// get the path to our Data/plist file
NSString *plistPath = [documentsPath stringByAppendingPathComponent:#"a.plist"];
// set the variables to the values in the text fields
NSMutableArray *array;
// create dictionary with values in UITextFields
NSDictionary *plistDict = [NSDictionary dictionaryWithObject:#"c" forKey:#"Id"];
if ([[NSFileManager defaultManager] fileExistsAtPath:plistPath])
{
array = [[NSMutableArray alloc] initWithContentsOfFile:plistPath];
[array addObject:plistDict];
[array writeToFile:plistPath atomically:YES];
NSLog(#"array%#",array);
}
else
{
NSArray *array = [NSArray arrayWithObject:plistPath];
[array writeToFile:plistPath atomically:YES];
}
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];
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..
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
How to save the background color state of a button after its parent view is removed. So dat the next time i click on the button after the parent view is again loaded I can change the initial color of the button.
You can go for PList.
Try this:-
In AppDelegate.h
+(NSString *)getValueFromPlist:(NSString *)key filename:(NSString *)filename;
+(NSString *)dataFilePath:(NSString *)filename;
In AppDelegate.m
+(NSString *)getValueFromPlist:(NSString *)key filename:(NSString *)filename
{
NSString *path = [AppDelegate dataFilePath:filename];
//NSLog(#"\npath - %#\n", path);
NSDictionary *dict = [[NSDictionary alloc]initWithContentsOfFile:path];
//NSLog(#"\nparsing- %#", dict);
return [dict valueForKey:key];
}
+ (NSString *)dataFilePath:(NSString *)filename {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDirectory = [paths objectAtIndex:0];
return [docDirectory stringByAppendingPathComponent:filename];
}
In yourViewController.m, use this code to save value in plist :-
NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
[dict setValue:#"Your Value to be saved" forKey:#"value"];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"nameOfPlist.plist"];
[dict writeToFile:path atomically:YES];
Next time, get the value from plist and use it your code :-
NSString *strValue = [AppDelegate getValueFromPlist:#"value" filename:#"nameOfPlist.plist"];