22

I am implementing a application based on web services. In that I need to add a string as property in .plist and I need to get the value from the .plist whenever I need in the code.

1

6 Answers 6

37

Here is a code sample:

NSString *path = [[NSBundle mainBundle] pathForResource: @"YourPLIST" ofType: @"plist"]; 
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile: path];
id obj = [dict objectForKey: @"YourKey"];
3
  • 1
    Hi I got the path null can you please help me
    – sekhar
    Oct 13, 2010 at 17:30
  • Your plist file must be added to the project, then you must set its exact name (including case) in the code above.
    – jv42
    Oct 14, 2010 at 8:51
  • 1
    @BenC.R.Leggiero Once you've executed the second line, you've got a regular NSDictionary that you can use as any other dictionary.
    – jv42
    Apr 20, 2015 at 16:33
21
NSBundle* mainBundle = [NSBundle mainBundle]; 

 

// Reads the value of the custom key I added to the Info.plist
NSString *value = [mainBundle objectForInfoDictionaryKey:@"key"];

//Log the value
NSLog(@"Value = %@", value);

// Get the value for the "Bundle version" from the Info.plist
[mainBundle objectForInfoDictionaryKey:@"CFBundleVersion"];

// Get the bundle identifier
[mainBundle bundleIdentifier];
6
NSURL *url = [[NSBundle mainBundle] URLForResource:@"YOURPLIST" withExtension:@"plist"];
NSArray *playDictionariesArray = [[NSArray alloc ] initWithContentsOfURL:url];

NSLog(@"Here is the Dict %@",playDictionariesArray);

or you can use following also

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"Sample.plist"];
2

Get from plist is very simple.

NSString *path = [[NSBundle mainBundle] pathForResource:@"SaveTags" ofType:@"plist"];
if (path) {
    NSDictionary *root = [NSDictionary dictionaryWithContentsOfFile:path];
}

If you want to add something to a plist, maybe you can find the answer here: How to write data in plist?

But if you only want save some message in you app, NSUserDefaults is the better way.

1

You can not do this. Any Bundle wether it is iOS or Mac OS is readonly, you can only read to it and you can't create files, write or do anything with the files in a bundle. This is part of the Security features of apple. You can use the NSDocumentsDirectory to writr and read your stuff you need for your app

0

Swift

I know this was asked 12+ years ago. But this was the first SO question to come up via google. So to save time for everyone, here is how to do this in swift:

struct Config {

    // option 1
    static var apiRootURL: String {
        guard let value  = (Bundle.main.object(forInfoDictionaryKey: "BASE_URL") as? String), !value.isEmpty else {
            fatalError("Base URL not found in PLIST")
        }
        return value
    }

    // option 2
    static var databaseName: String {
        guard let value  = (Bundle.main.infoDictionary?["DB_NAME"] as? String), !value.isEmpty else {
            fatalError("DB NAME not found in PLIST")
        }
        return value
    }
    
}

Notice the 2 functions use slightly diffrent methods to access the plist. But in effect they are almost the same.

In theory there might not be a plist. Hence infoDictionary is optional. But in this case the first method will also return an unexpected value, resulting in an error.

One actual difference as noted by Apple:

Refering to Bundle.main.object(forInfoDictionaryKey: "BASE_URL")

Use of this method is preferred over other access methods because it returns the localized value of a key when one is available.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.