How to read a localized plist as Dictionary? - ios

How can I read a localised plist as dictionary? This felt promising …
if let path = NSBundle.mainBundle().pathForResource("LocalizedDictionary", ofType: "plist", inDirectory: ???, forLocalization: "en") {
localizedDictionary = NSDictionary(contentsOfFile: path)
return localizedDictionary
}
But I apparently I had issues getting right path for the inDirectory parameter. Is this the right approach?
How do I get the localised version of a plist file as Dictionary?
Note: I am not using NSLocalizedString as I had issues without switching languages while the app is running.

Here's something that should help.
Create a NSDictionary class extension like this.
It can be tweaked and adapted to your needs ( to load 'strings' dictionaries for exemple, or passing the locale in a parameter… )
#implementation NSDictionary (NSDictionary_Extras)
+(NSDictionary*)dictionaryWithName:(NSString*)name
{
NSDictionary *dict = NULL;
NSURL *dictURL = [[NSBundle mainBundle] URLForResource:name withExtension:#"plist"];
if (dictURL) {
dict = [NSDictionary dictionaryWithContentsOfURL:dictURL];
}
return dict;
}
+(NSDictionary*)localizedDictionaryWithName:(NSString*)name
{
NSDictionary *localizedDict = NULL;
NSString *localizationName = [[NSLocale preferredLanguages] objectAtIndex:0];
NSURL *localizedDictURL = [[NSBundle mainBundle] URLForResource:name withExtension:#"plist" subdirectory:[NSString stringWithFormat:#"%#.lproj",localizationName]];
// If resource doe not exist, we try at root ( resource must have a 'base' localization, or not being localized )
if (!localizedDictURL) {
localizedDictURL = [[NSBundle mainBundle] URLForResource:name withExtension:#"plist"];
}
if (localizedDictURL) {
localizedDict = [NSDictionary dictionaryWithContentsOfURL:localizedDictURL];
}
return localizedDict;
}
#end
Then you simply load a plist using :
NSDictionary* myDict = [NSDictionary dictionaryWithName:#"myDict"]
Or a localized plist with
NSDictionary* myDict = [NSDictionary localizedDictionaryWithName:#"myDict"].
I import this extra in all my projects.

You don't need to change any code.
xCode's resource localisation is neat and can frequently be completely transparent to you. You just ask the main bundle for resource urls as normal and load from them. NSBundle performs any mapping required to give you localised resources.
There's a tutorial here that got me going. It's a little out of date, but none of the significant details have changed.

Related

iOS - extracting data from a plist not working

This is a routine exercise. I have done it a number of times in my current project and it has worked fine. I copied the code line for line, same initializations. My plist data goes into a dictionary but then it does not go into its respective arrays in their initializations. I have a method called initArraysPlist
-(void)initArraysPlist{
NSString *path1 = [[NSBundle mainBundle] pathForResource:#"trainerProfile" ofType:#"plist"];
// Load the file content and read the data into arrays
NSDictionary *dict1 = [[NSDictionary alloc] initWithContentsOfFile:path1];
trainerNames = [dict1 objectForKey:#"Names"];
trainerIcons = [dict1 objectForKey:#"Icons"];
trainerFactSheet= [dict1 objectForKey:#"Fact Sheet"];
trainerFocus = [dict1 objectForKey:#"Focus"];
trainerContactInfo= [dict1 objectForKey:#"Contact Info"];
}
Ive done this a few times and it currently works in my code. all the values are correct. Ive checked it many times. when
Please read the comments for the each line.
NSString *path1 = [[NSBundle mainBundle] pathForResource:#"trainerProfile" ofType:#"plist"]; // **check if your plist is actually added in Bundle.If its there move to second line , if not then add plist in bundle.**
NSDictionary *dict1 = [[NSDictionary alloc] initWithContentsOfFile:path1];// **if plist is added in bundle , then check if you are getting value for dict1 . If no then you might be making some mistake in plist structure.**
For more clarifications please post your plist if possible.
Please try this code it may be helpful to you
// Read plist from bundle and get Root Dictionary out of it
NSDictionary *dictRoot = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"data" ofType:#"plist"]];
// Your dictionary contains an array of dictionary
// Now pull an Array out of it.
NSArray *arrayList = [NSArray arrayWithArray:[dictRoot objectForKey:#"catlist"]];
// Now a loop through Array to fetch single Item from catList which is Dictionary
[arrayList enumerateObjectsUsingBlock:^(id obj, NSUInteger index, BOOL *stop) {
// Fetch Single Item
// Here obj will return a dictionary
NSLog(#"Category name : %#",[obj valueForKey:#"category_name"]);
NSLog(#"Category id : %#",[obj valueForKey:#"cid"]);
}];

How do I get values from a tiered .plist?

I've already looked at Parse Plist (NSString) into NSDictionary and deemed it to be not a duplicate, as that question and its answer do not address my concerns.
I have a .plist file in the file system structured like this:
The source code of this .plist file looks like this:
{
"My App" = {
"Side Panel" = {
Items = {
Price = "#123ABC";
};
};
};
}
I know how to get an item in the Root like this:
[[NSBundle mainBundle] pathForResource:#"filename" ofType:#"plist"];
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];
NSString value = [dict objectForKey:#"key"]);
But what if the structure is like mine, with tiered dictionaries? How do I get the value of Price?
I would like to do this all in one method, ideally like this:
Calling
NSString *hexString = [self getColorForKey:#"My App.Side Panel.Items.Price"];
Definition
- (NSString *) getColorForKey: (NSString *)key
{
NSArray *path = [key componentsSeparatedByString:#"."];
NSDictionary *colors = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"Colors" ofType:#"plist"]];
NSString *color = #"#FFFFFF"; // white is our backup
// What do I put here to get the color?
return color;
}
Here's the solution that worked for me:
+ (NSString*) getHexColorForKey:(NSString*)key
{
NSArray *path = [key componentsSeparatedByString:#"."];
NSDictionary *colors = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"Colors" ofType:#"plist"]];
NSString *color = #"#FFFFFF";
for (NSString *location in path) {
NSObject *subdict = colors[location];
if ([subdict isKindOfClass:[NSString class]])
{
color = (NSString*)subdict;
break;
}
else if ([subdict isKindOfClass:[NSDictionary class]])
{
colors = (NSDictionary*)subdict; // if it's a dictinoary, our color may be inside it
}
else
{
[SilverLog level:SilverLogLevelError message:#"Unexpected type of dictinoary entry: %#", [subdict class]];
return color;
}
}
return color;
}
where key is an NSString that matches /^[^.]+(\.[^.]+)*$/, meaning it looks like my targeted #"My App.Side Panel.Items.Price".
Yes I understand what you're looking to accomplish; thank you for the clarification. I will however add that the resources and advice I have written do provide the necessary information resolve your problem.
That said, the following gets your dictionary:
NSURL *plistURL = [[NSBundle mainBundle] URLForResource:#"Info" withExtension:#"plist"];
NSData *plistData = [NSData dataWithContentsOfURL:plistURL];
NSDictionary *tieredPlistData = [NSPropertyListSerialization propertyListWithData:plistData
options:kCFPropertyListImmutable
format:NULL
error:nil];
Then, if we're interested in the information contained in Items
NSDictionary *allItemsDictionary = tieredPlistData[#"My App"][#"Side Panel"][#"Items"];
Assuming that Items will contain a number of objects, you could use
NSArray *keys = [allItems allKeys];
for(NSString *key in keys){
NSString *colorValue = allItemsDictionary[key];
// do something with said color value and key
}
Or, if there is a single value you need, then just reference that key
NSString *colorForPriceText = allItemsDictionary[#"Price"];
But a few tips:
It's generally considered a better idea to keep frequently accessed values in code instead of a plist/file that is loaded at runtime.
That said, you wouldn't put your call to load from NSBundle in the same method you would use to query a specific value. In your example, every time you need a color, you end up re-accessing NSBundle and pile on unneeded memory allocations. One method would load the plist into an iVar NSDictionary and then that NSDictionary would be used separately by another method.

How could I have .plist path inside iPad / iPhone

How could I have a complete list of each .plist path (inside iPhone/iPad) ?
In iOS Simulator I'm using it : ls -l ~/Library/Preferences/com.apple.*.plist
My goal is to find a specific key and read the boolean value in preferences.
Because I need to know if it is enable or not.
It is something that is missing in SDK but is existing in preferences.
NSLog (#"%#", [[NSBundle mainBundle] pathsForResourcesOfType:#"plist" inDirectory:#"/"]);
The method returns an NSArray.
You need to create the plist first. All you have to do is access it when you need it.
NSArray *thePlist;
NSString *plist = [[NSBundle mainBundle] pathForResource: #"plistFileName" ofType: #"plist"];
thePlist = [[NSArray alloc] initWithContentsOfFile: plist];
Thank you Daniel A. White , Jahm and Kedar!
I code this to summarize each comment :
With an extra, add an example to read UISupportedInterfaceOrientations values.
NSLog(#"Preferences plists from apple (inside iPhone/iPad) are not accessible from code");
NSLog (#"Accessible plist paths : %#", [[NSBundle mainBundle] pathsForResourcesOfType:#"plist" inDirectory:#"/"]);
NSArray *pList = [[NSBundle mainBundle] pathsForResourcesOfType:#"plist" inDirectory:#"/"];
for (int i = 0; i < [pList count]; i ++)
{
NSString *plistPath = [pList objectAtIndex:i];
NSDictionary *plistDictionary = [NSDictionary dictionaryWithContentsOfFile:plistPath];
NSMutableArray *nameString = [plistDictionary objectForKey:#"UISupportedInterfaceOrientations"];
for (NSString *n in nameString)
{
NSLog(#"Supported Interface : %#",n);
}
}

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.
Here is a code sample:
NSString *path = [[NSBundle mainBundle] pathForResource: #"YourPLIST" ofType: #"plist"];
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile: path];
id obj = [dict objectForKey: #"YourKey"];
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];
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"];
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.
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
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.

iPhone programming, loading plist data, access it

I have a plist in the following form:
Root (array)---> item 1 (dictionary) ----> Sch (string)
---> Name (string)
----> price (number)
----> item 2 (dictionary)----> .....same as item 1
How can I access each row (item1 to ...) and the its child (Sch, Name etc.)? One at a time?
I use:
NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *finalPath = [path stringByAppendingPathComponent:#"Data.plist"];
NSDictionary *plistData = [[NSDictionary dictionaryWithContentsOfFile:finalPath] retain];
to load the file. How should I go about accessing each child?
What I am trying to do is, I have a NSString *message, what I want to do is to search the whole plist for matching string and display the whole item 1. Any suggestion?
When you initialize a collection from a plist, the type is the root level object. Therefore you would not initialize a dictionary but an array like so:
NSArray *plistData = [[NSArray arrayWithContentsOfFile:finalPath] retain];
Then you would access it like this:
NSString *sch;
NSString *name;
NSString *price;
for (NSDictionary *aDict in plistData) {
sch = [aDict objectAtKey:"Sch"];
name = [aDict objectAtKey:"Name"];
price = [aDict objectAtKey:"price"];
//.. do whatever
}
Here is a link to a discussion that provided a very good example of how to access your data and which type of storage schemes would be more beneficial under certain circumstances. #TechZen's solution is on target, I just thought this question would add an additional resource. Hope this helps!

Resources