简体   繁体   中英

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 eg

<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.eg FacebookAppID is "abc" for debug mode and "xyz" for release mode.

Set your custom variable in info.plist as shown below. I have taken "HockeyAppID" as example 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.

在此处输入图片说明

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.

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.

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM