19

Whenever I want to get data from a plist file I use the following code:

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"FILE_NAME" ofType:@"plist"];
NSDictionary *plistData = [NSDictionary dictionaryWithContentsOfFile:filePath]; 

But now I'm trying to read in data from the Info plist, and filePath is nil. Is there a different way to get data from the Info plist?

Darren
  • 10,091
  • 18
  • 65
  • 108
  • Possible duplicate of [iOS: Access app-info.plist variables in code](http://stackoverflow.com/questions/9530075/ios-access-app-info-plist-variables-in-code) – thegrinner Aug 17 '16 at 12:46

3 Answers3

53

From an earlier SO answer of mine. Attributes from the info.plist for your project are directly accessible by the following...

[[NSBundle mainBundle] objectForInfoDictionaryKey:key_name];

Your filePath is nil simply because it can't find the file - check spellings & check if the file you are trying to read from is actually in the bundle etc.

Community
  • 1
  • 1
Damo
  • 12,840
  • 3
  • 51
  • 62
  • I was able to get the plist using [NSBundle mainBundle].infoDictionary. The info plist was not in the Copy Bundle Resources list for the target. Is the info plist stored somewhere else? – Darren Mar 21 '12 at 13:34
  • Swift version if anyone needs it `Bundle.main.object(forInfoDictionaryKey: "key_name")` – J_B_UK Jan 16 '21 at 20:34
1

Replace

[[NSBundle mainBundle] pathForResource:@"FILE_NAME" ofType:@"plist"]

with

[[NSBundle mainBundle] pathForResource: @"Info" ofType: @"plist"]
codercat
  • 22,873
  • 9
  • 61
  • 85
Amr
  • 2,160
  • 1
  • 15
  • 8
1

I do not think there is another way (unless it is the info.plist file then see Damo's comment), instead I would focus on figuring out why the filePath is nil, perhaps the plist file is no longer under target>build phases>copy bundle resources?

Oscar Gomez
  • 18,436
  • 13
  • 85
  • 118
  • 1
    I was able to get the plist using [NSBundle mainBundle].infoDictionary. The info plist was not in the Copy Bundle Resources list. I guess there is something special about the info plist? – Darren Mar 21 '12 at 13:30
  • Well spotted - I never would have thought that but upon checking my current project the info.plist is also unchecked for copy – Damo Mar 21 '12 at 13:35