Get and update plist value - ios

I created a plist titled "appData.plist" in my supporting files section of Xcode. I have a key called "keyone" and am trying to get the value of it. I'm trying to log the value to console with NSLog but its not working.
How do you do this? I've searched google and there are a lot of way but none work for me.
NSString *myFile = [[NSBundle mainBundle] pathForResource: #"appData" ofType: #"plist"];
NSDictionary *myDict = [NSDictionary dictionaryWithContentsOfFile:myFile];
NSString *viewCover = [[NSBundle mainBundle] objectForInfoDictionaryKey:#"keyone"];
This is what i've tried but the whole thing ends in errors, I can't log it and I get an 'unusable variable' error.

The objectForInfoDictionaryKey method retrieves a value from your app's Info.plist file, which isn't what you want -- you want to retreive it from your dictionary myDict. You mentioned that the value is actually a boolean, so replace the last line with
BOOL viewCover = [[myDict objectForKey:#"keyOne"] boolValue];

Related

Define Entity's Attribute's Value

I am trying to edit rawURL so that it looks in the correct folder within my application bundle. I looked at Apple's documentation, but when I opened up my application's .xcdatamodeld, the attribute whose String value I'm looking to edit, didn't appear to be set to anything:
I looked in the entire code base, and I couldn't find it set anywhere programatically. However, I put a print statement in the code I'm looking to change, and it does in fact print a string. I want to change the value of this string so that it looks in FindSpecies/[species name]/[img file] not FindSpecies/species/[species name]/images/[img file].
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString* cachePath = [paths objectAtIndex:0];
NSString* resourcePath = [[NSBundle mainBundle] resourcePath];
NSLog(#"self.rawurl: %#", self.rawURL);
NSString* stringURL = [self.rawURL stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
NSLog(#"stringURL: %#", stringURL);
NSURL* url = [NSURL URLWithString:stringURL];
NSString* path = [url path];
NSString* query = [url query];
//...
returnString = [NSString stringWithFormat:#"%#/%#%#", resourcePath, kThumbnailDirectoryName, path];
Can I edit rawURL if I don't even know where/how its set in the first place?
EDIT: This app was built in 2011 by my professor's former grad students. I am in the process of modernizing it.
There's no reason to expect rawURL to have a value in the model editor you show in your screenshot. That defines the schema for the Core Data persistent store, not the data. It might have default values but those aren't required.
If rawURL has a value then it's being set somewhere. You have the source code, so you should be able to find it. If you can't, look again, because you can see it's happening somewhere. If your entity has a subclass of NSManagedObject, it might help to put a breakpoint at the rawURL property declaration in that class. The breakpoint should trigger any time the value changes.
You can of course also change it any time you want on any object instance that has the property. Just assign a value, like any other string property.

Unable to read plist from a bundle outside my own app

What am I doing wrong? Trying to read Info.plist of Calendar.app into an array but I get nil array. Here is my code:
NSBundle* xyzbundle = [NSBundle bundleWithPath:#"/Applications/Calendar.app/Contents"];
// Path to the plist
NSString *path = [xyzbundle pathForResource:
#"Info" ofType:#"plist"];
// Build the array from the plist
NSMutableArray *array2 = [[NSMutableArray alloc] initWithContentsOfFile:path];
// Show the string values
for (NSString *str in array2)
NSLog(#"--%#", str);
Thank you for your help.
You cant do it. iOS kernel creates a sandbox environment for each of the application. Any application can not read/write data of other application. There are some possible exceptions but in general you cant do it.

iOS - Issue with reading from a plist

I'm having an issue with reading from a plist I can't seem to figure.
My plist list looks like this (note i've simplified it for examples sake):
Then I'm reading code like this:
NSString *path = [[NSBundle mainBundle] pathForResource:#"ContactDetails" ofType:#"plist"];
NSMutableDictionary *myDictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
NSArray* allmyKeys = [myDictionary allKeysForObject:#"Name1"];
Any ideas - the issue i keep facing is allmyKeys shows up as containing 0 objects. On debugging myDictionary is correctly populated so not sure why it doesn't work.
Many Thanks
The problem is with this line:
NSArray* allmyKeys = [myDictionary allKeysForObject:#"Name1"];
#"Name1" is a key in your dictionary, not a value. Even if it were a value, the string #"Name1" is a different object from the string with the same value in myDictionary, so this call would not do what you expect.
You probably want to access the dictionary by doing
NSDictionary *userDetails = myDictionary[#"Name1"];

NSSring from plist created from NSArray

I create a NSArray and write to a file: a.plist.
I use NSString: initWithContentsOfFile, and I can see the content in xml.
Then, I add a.plist to another project
and then I use NSString: initWithContentsOfFile to get the xml string.
filePath = [[NSBundle mainBundle] pathForResource:#"a" ofType:#"plist"];
NSString *plistStr = [[NSString alloc]initWithContentsOfFile:filePath];
However, it failed to recreate the xml string.
I user NSArray to test:
NSArray *plist2Array = [[NSArray alloc]initWithContentsOfFile:filePath];
But it successfully.
I think it may result from "Text Encoding" when I add it to another project.
The problem is I tried UTF8,UTF16 and so on.
I still can't find solution.
Hope for your help,thanks!
I find it!
rename the file: a.plist ->a.txt(or any else),
then in another project you can get the xml string.

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.

Resources