sekhar
sekhar

Reputation: 263

How to add and get the values from .plist in iOS

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.

Upvotes: 22

Views: 25458

Answers (6)

SimeonRumy
SimeonRumy

Reputation: 403

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.

Upvotes: 0

VSN
VSN

Reputation: 2361

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

Upvotes: 6

Janosch Hübner
Janosch Hübner

Reputation: 1694

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

Upvotes: 1

kimimaro
kimimaro

Reputation: 1263

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.

Upvotes: 2

Nikhil Dinesh
Nikhil Dinesh

Reputation: 3409

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

Upvotes: 21

jv42
jv42

Reputation: 8593

Here is a code sample:

NSString *path = [[NSBundle mainBundle] pathForResource: @"YourPLIST" ofType: @"plist"]; 
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile: path];
id obj = [dict objectForKey: @"YourKey"];

Upvotes: 37

Related Questions