Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Code value in the info.plist file xcode ios

I want to use some custom constants in info.plist file to use it globally e.g

<key>FacebookAppID</key>
<string>$(my_custom_constant)</string>
  1. how to make this constant?
  2. how can I differentiate it by selecting debug and release mode.e.g. FacebookAppID is "abc" for debug mode and "xyz" for release mode.
like image 635
Aqeel Ahmad Avatar asked Mar 19 '26 19:03

Aqeel Ahmad


2 Answers

Set your custom variable in info.plist as shown below. I have taken "HockeyAppID" as example here.

enter image description here

Next, Add a variable in Build Settings under "User-Defined" for Debug and Release configuration in your case as shown below. Here, I have my own four different configurations.

enter image description here

As you know, different configuration values will be loaded at runtime based on the settings in scheme. In order to access HockeyAppId for Debug / Release configuration from info.plist, do the following.

 enum InfoPlistKey {
   static let hockeyappID = "HockeyAppID"
 }

 struct AppSettings {

   private static var infoDict: [String: Any] {
      if let dict = Bundle.main.infoDictionary {
          return dict
      } else {
          fatalError("Info Plist file not found")
      }
   }

   static let hockeyAppID = infoDict[InfoPlistKey.hockeyappID] as! String
 }

Now, you can access HockeyAppId value from Info.plist as ,

let identifier = AppSettings.hockeyAppID

Please let me know in case of any issues.

like image 179
Anand Avatar answered Mar 22 '26 08:03

Anand


You can create the variable by adding it as a "User-Defined Setting" to your target, in Build Settings. You can then set the variable value to different things for each of your build configurations.

Please see attached screenshot. You can ignore my Beta Prod and Beta Test configurations, as they probably don't apply to your situation.

enter image description here

like image 38
rodskagg Avatar answered Mar 22 '26 07:03

rodskagg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!