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

6

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.

Linville answered 13/10, 2010 at 8:26 Comment(1)
A
37

Here is a code sample:

NSString *path = [[NSBundle mainBundle] pathForResource: @"YourPLIST" ofType: @"plist"]; 
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile: path];
id obj = [dict objectForKey: @"YourKey"];
Arundel answered 13/10, 2010 at 8:32 Comment(3)
Hi I got the path null can you please help meLinville
Your plist file must be added to the project, then you must set its exact name (including case) in the code above.Arundel
@BenC.R.Leggiero Once you've executed the second line, you've got a regular NSDictionary that you can use as any other dictionary.Arundel
T
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];
Tammietammuz answered 8/5, 2012 at 10:50 Comment(0)
F
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"];
Flatt answered 26/6, 2012 at 11:33 Comment(0)
M
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.

Melisa answered 2/11, 2012 at 12:12 Comment(0)
S
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

Selfsufficient answered 23/12, 2012 at 18:43 Comment(0)
H
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.

Homothermal answered 11/12, 2022 at 8:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.