7

How to set Info.plist Values based on Build Configuration in Xcode

 2 years ago
source link: https://sarunw.com/posts/set-info-plist-value-per-build-configuration/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

How to set Info.plist Values based on Build Configuration in Xcode

16 Mar 2023 ⋅ 3 min read ⋅ Xcode Development

Table of Contents

Info.plist is a file that contains information about an app, e.g., app name, app version, or even API Key for third-party service.

If you use build configuration to separate setting between development, stating, and production, you might want to configure Info.plist value based differently based on build configuration.

In this article, we will learn how to do that.

I will give you two examples.

  1. Setting a value that Xcode already provided. I will set a different app's name based on the build configuration.
  2. Setting a custom value. I will set an imaginary API key.

Setting a predefined value

If you open Info.plist or Info tab, under the "Bundle name", which is a key for the app name, you will see the value set to $(PRODUCT_NAME).

infoplist-build-configuration-bundle-name.png

Dolla sign ($) and a build settings key between curly brackets (${BUILD_SETTINGS_KEY}) is a way to reference a build setting value.

In this case, our bundle name is set to the PRODUCT_NAME key in the build setting.

So, we need to modify the PRODUCT_NAME value in the build setting if we want to change the app name.

To do that.

  1. Select the "Build Settings" tab.
  2. Search for "product name". The key in the build setting is usually named after the key you see in the Info.plist, which is PRODUCT_NAME in this case.
  3. Click on the arrow on the left of the "Product Name". You will see two nested rows with the key named after the Xcode build configuration, Debug, and Release.
  4. Double-click on the value to change it. In this case, I change the app name for debug configuration to "Sarunw-Sample Debug".
infoplist-build-configuration-product-name.png

That's all you need to do to change the app name based on the build configuration.

You can easily support sarunw.com by checking out this sponsor.

Build, manage, and grow in-app purchases:

Sponsor sarunw.com and reach thousands of iOS developers.

Setting a custom value

If you need to define a new Info.plist key and new Build configuration value, you can also do that.

In this example, I will create a new Info.plist key for an imaginary API Key, SARUNW_API_KEY.

Create a new Info.plist key

To create a new Info.plist key.

  1. Open the Info tab.
  2. Hover on the last row and click on the plus button (+).
  3. New row will appear. Name it whatever you want. In this case, I named it SARUNW_API_KEY.
Create a new Info.plist key.

Create a new Info.plist key.

  1. Set the value by double-clicking on the value column. I want to use different values based on the build setting, so I will put a reference to a key in the build setting. In this case, I put it under the same name, ${SARUNW_API_KEY}.
Set value to value in the build setting.

Set value to value in the build setting.

At this point, we got new Info.plist key with the value point to the SARUNW_API_KEY key in the build setting.

The last thing we need to do is create this key in the build setting.

Create a user-defined setting

To create a new key in the build setting.

  1. Open the Build Settings tab.
  2. Click on the plus button (+) at the upper left, then select "Add User-Defined Setting". Enter the name of the new key. In this case, I named it SARUNW_API_KEY.
Add User-Defined Setting.

Add User-Defined Setting.

  1. Expand the new setting, and set the value based on the build configuration.
infoplist-build-configuration-new-build-configuration-value.png

With this setting, you can have different SARUNW_API_KEY based on debug and release configuration.

You can test it by reading the value from the Info.plist file.

print(Bundle.main.object(forInfoDictionaryKey: "SARUNW_API_KEY"))
// Debug configuration
// DEBUG-API-KEY

// Release configuration
// RELEASE-API-KEY

Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK