Middle
How to change the app icon depending on the environment?
sobes.tech AI
Answer from AI
Depending on the environment (e.g., Dev, Staging, Prod), you can use:
- Creating separate targets for each environment.
- Using configuration files (
.xcconfig). - Adding custom keys in
Info.plist.
Process:
- Create duplicates of the main app target for each environment.
- In the settings of each new target, change the
Info.plist Fileor add theCFBundleIconFileskey and specify the icon file name. - In
.xcconfigfiles for each environment, define a variable pointing to the respectiveInfo.plist. - In the main target's Build Settings, set
Info.plist Fileto${INFO_PLIST_FILE}(whereINFO_PLIST_FILEis a variable from.xcconfig).
Example .xcconfig for Dev:
// DEV.xcconfig
INFO_PLIST_FILE = Info_Dev.plist
Example Info_Dev.plist:
<!-- Info_Dev.plist -->
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
...
<key>CFBundleIcons</key>
<dict>
<key>CFBundlePrimaryIcon</key>
<dict>
<key>CFBundleIconFiles</key>
<array>
<string>AppIconDev</string> // Asset name for Dev icon
</array>
</dict>
</dict>
...
</dict>
</plist>
An alternative approach without duplicating targets is to use build scripts that modify Info.plist depending on the current build configuration. However, using separate targets is more explicit and maintainable.
For managing icon assets, you can use separate Asset Catalogs for each target or configuration, or name icon sets uniquely (AppIconDev, AppIconStaging, AppIconProd) and specify the appropriate name in Info.plist.