Reputation: 5872
In my app, I want some of the values to be device specific.
In android, I could use different resource files, and the correct one would be used according to the device I'm running on.
How can I achieve a similar behavior in ios?
Note: I read about device specific values in plist, but I can't seem to get the correct one when running on an ipad.
This is how I saw I can read values (and obviously it's not working since I simply read from a dictionary):
NSString *path = [[NSBundle mainBundle] pathForResource:@"values" ofType:@"plist"];
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path];
int number = [[dict objectForKey:@"colsInPage"] intValue];
Edit:
Here is an example for what I'm looking for:
I have a UICollectionView and I want to calc width for each item according to the number of columns I want to have. in an iphone, I want 4 cols, and in Ipad I want 6.
Upvotes: 1
Views: 141
Reputation: 9341
You can use iOS Supports Device-Specific Resources to open one file on iPhone, and a different file on the iPad, like people commonly do with images, without having to any device checks yourself.
You would name the iPhone version colsInPage~iphone.plist
, and the iPad version colsInPage~ipad.plist
. In the program, have it open colsInPage.plist
, and iOS would pick the correct version automagically for you.
Upvotes: 2
Reputation: 2427
You could do that. We do something similar where we have an outer dictionary where the keys are the configuration, in your case that would be the device size or whatever you want the differentiating feature to be. Those keys link to other dictionaries who's keys are the coorospond to the different values we want. For example this is the meet an potatoes of what we do.
<dict>
<key>iphone</key>
<dict>
<key>collumsPerPage</key>
<string>5</string>
</dict>
<key>ipad</key>
<key>collumsPerPage</key>
<string>5</string>
</dict>
</dict>
the keys iPhone and iPad are just examples, you'll have to find out how to relate those keys to something you can figure out programattically. For example you could do
NSDictionary *plist = [NSDictionary dictionaryWithContentsOfFile:.......];
NSDictionary *valuesDict;
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
{
valuesDict = plist[@"iphone"];
}
int number = [[valuesDict objectForKey:@"colsInPage"] intValue];
...
Upvotes: 0
Reputation: 318804
If your needs are simple - one set of values for all iPhone/iPod touch devices and another set of values for all iPads, then you can simply create two plist files with names such as "values-iphone.plist" and "values-ipad.plist".
Then you code can be:
NSString *path;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
path = [[NSBundle mainBundle] pathForResource:@"values-ipad" ofType:@"plist"];
} else {
path = [[NSBundle mainBundle] pathForResource:@"values-iphone" ofType:@"plist"];
}
Upvotes: 0
Reputation: 7265
The "specific values in plist" you are referring to only applies to Info.plist
. The path you are building is to a plist filed named values.plist
.
All apps require an Info.plist
. It will be named something like `MyCoolApp-Info.plist". This file specifies specific values for attributes related to the application such as orientations supported, status bar display, etc.
Add these raw values to your Info.plist
colsInPage 4
colsInPage~ipad 6
Now, provided you have put the keys in correctly, change your code as follows:
NSDictionary *dict = [[NSBundle mainBundle] infoDictionary];
int number = [[dict objectForKey:@"colsInPage"] intValue];
This will input 4 into number when you are on any device other than iPad (i.e. iPhone or iPod) and 6 when ran on an iPad.
Upvotes: 0