1

I have multiple info.plist files and would like to access them programatically. I read here how this could be done:

https://sarunw.com/posts/how-to-read-info-plist/

But when I try:

let infoPlistPath = Bundle.main.url(forResource: "Myplist-Info", withExtension: "plist")
print(infoPlistPath)

it returns nil.

I have tried adding the plist to the respective target with the same result. I have also tried adding the plist to the setting copy bundle resource. But it still returns nil.

How do I make this happen?

Joakim Sjöstedt
  • 824
  • 2
  • 9
  • 18
  • 1
    Is that the real name of your plist file? Case sensitive, etc? Could you screenshot your XCode view? – Larme Nov 16 '21 at 09:44
  • No it's not, I just wrote something else here on SO because of company policies and so on. I have tried multiple info.plist files and it's all the same... If it helps, I'm trying to access them in a unitTest. – Joakim Sjöstedt Nov 16 '21 at 09:57
  • 1
    https://stackoverflow.com/questions/19151420/load-files-in-xcode-unit-tests ? – Larme Nov 16 '21 at 10:00
  • Thanks, but I tried putting it outside of the test in a regular viewController and it's the same thing :/ still nil – Joakim Sjöstedt Nov 16 '21 at 10:14
  • Compile the app then open the IPA file: are the plist files in there? Easy way to test if the files are properly copied in the bundle. – Eric Aya Nov 16 '21 at 14:13

1 Answers1

0

I have attached the code to get all properties of Info.plist.

func getDataFromInfoPlist(){
    if let path = Bundle.main.path(forResource: "Info", ofType: "plist") {
       let infoListValue = NSDictionary(contentsOfFile: path)
        do {
            let JsonData = try JSONSerialization.data(withJSONObject: infoListValue as Any, options: .prettyPrinted)
            let val = NSString(data: JsonData, encoding: String.Encoding.utf8.rawValue)! as String
            print(val)
        } catch {
            print(error.localizedDescription)
        }

    }
    print(nsDictionary as Any)
}
sid_patel_1211
  • 151
  • 1
  • 7