Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mac OSX How can I add plist to bundle and modify the plist

I have a plist and I copy the plist to my xcode project but it seems like the file is not in bundle:

NSDictionary *dict = [[NSBundle mainBundle] infoDictionary];
nslog (@"dict %@",dict);

but the file plist file is not showing. Question, how can I add the file to project in a way to see in the bundle? also any of you knows how can I modify the plist and save the changes?

I'll really appreciated your help

P.S. I'm using Xcode 5.

like image 851
user2924482 Avatar asked Dec 03 '25 14:12

user2924482


2 Answers

You can create a plist file in your bundle and modify its contents as:

NSString *bundlePath = [[NSBundle mainBundle]bundlePath]; //Path of your bundle
NSString *path = [bundlePath stringByAppendingPathComponent:@"MyPlist.plist"]; 
NSFileManager *fileManager = [NSFileManager defaultManager];

NSMutableDictionary *data;

if ([fileManager fileExistsAtPath: path]) 
{
    data = [[NSMutableDictionary alloc] initWithContentsOfFile: path];  // if file exist at path initialise your dictionary with its data
}
else
{
    // If the file doesn’t exist, create an empty dictionary
    data = [[NSMutableDictionary alloc] init];
}

//To insert the data into the plist
int value = 5;
[data setObject:[NSNumber numberWithInt:value] forKey:@"value"];
[data writeToFile: path atomically:YES];
[data release];

//To retrieve the data from the plist
NSMutableDictionary *savedData = [[NSMutableDictionary alloc] initWithContentsOfFile: path];
int savedValue;
savedValue = [[savedData objectForKey:@"value"] intValue];
NSLog(@"%i",savedValue);
like image 96
Neha Avatar answered Dec 06 '25 04:12

Neha


infoDictionary will return Info.plist file. You should use pathForResource:ofType: method of NSBundle class.

NSString *commonDictionaryPath;
if (commonDictionaryPath = [[NSBundle mainBundle] pathForResource:@"CommonDictionary" ofType:@"plist"])  {
    theDictionary = [[NSDictionary alloc] initWithContentsOfFile:commonDictionaryPath];
}

How can I add plist to bundle
Drag your plist to "Copy Bundle Resources" phase of your build target

modify the plist
Copy the plist to your app's Documents folder and modify it there.

like image 35
Parag Bafna Avatar answered Dec 06 '25 06:12

Parag Bafna



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!