How to add a value dynamically in Info.Plist file

86 views Asked by At

I want to add audio property in UIBackgroundModes in Info.plist based on a condition.

My react-native app is a single codebase for multiple apps. I need audio property only for one of the brands which is why one of the brand apps got rejected for AppStore release. enter image description here

1

There are 1 answers

0
sakshya73 On

After some research, I got to know that the info.plist file can't be changed on the runtime. So, multiple solutions can be used during the build time.

For dynamic string values you can directly use https://www.npmjs.com/package/react-native-config.

It will look something like this. enter image description here

First -
You can utilize preprocessor macros in your build configuration.

To conditionally add the audio value under UIBackgroundModes in the Info.plist during build time, you can follow a similar approach to the one outlined before. Here's how you can do it:

  1. Create a Build Configuration: Just like before, create a new build configuration in Xcode or modify an existing one to suit your needs.

  2. Define Preprocessor Macros: Define a preprocessor macro for your new build configuration. For instance, you can define a macro like CUSTOM_AUDIO_ENABLED for the "CustomAudioEnabled" configuration.

  3. Use a Script to Modify Info.plist: Write a script that runs as a build phase. This script should modify the Info.plist file to include the audio value under UIBackgroundModes only when the specific build configuration is active.

if [ "$CONFIGURATION" == "CustomAudioEnabled" ]; then
    /usr/libexec/PlistBuddy -c "Add :UIBackgroundModes array" Info.plist
    /usr/libexec/PlistBuddy -c "Add :UIBackgroundModes:0 string audio" Info.plist
fi

Second -
You can utilize build scripts We had a pipeline to build the app. So, this worked for me.

  1. Create a Bash Script: Write a bash script that will handle the creation of the new build configuration and any associated tasks. You can name this script something like create_build_configuration.sh.

  2. Script Steps: Within your script, you'll need to perform the following steps:

    a. Use commands like plutil or PlistBuddy to modify the project file (.xcodeproj) or workspace file (.xcworkspace) to add a new build configuration.

    b. You'll also need to modify the Info.plist file to add the necessary entries conditionally based on this new build configuration.

    c. Optionally, you can also handle other tasks related to configuring the Xcode project, such as setting build settings or updating scheme configurations.

  3. Run the Script: Once you've written your script, you can execute it from the command line. Ensure that you have the necessary permissions to execute the script, and that you're running it from the correct directory where your Xcode project file is located.

  4. Verification: After running the script, open your Xcode project to verify that the new build configuration has been created and configured correctly, and that the Info.plist file has been updated as expected.

#!/bin/bash

# Add a new build configuration
xcodeproj_path="YourProject.xcodeproj"
configuration_name="CustomAudioEnabled"
xcodebuild -project "$xcodeproj_path" -target "YourTarget" -configuration "$configuration_name" clean
xcodebuild -project "$xcodeproj_path" -target "YourTarget" -configuration "$configuration_name" build

# Modify Info.plist based on the new configuration
if [ "$configuration_name" == "CustomAudioEnabled" ]; then
    /usr/libexec/PlistBuddy -c "Add :UIBackgroundModes array" Info.plist
    /usr/libexec/PlistBuddy -c "Add :UIBackgroundModes:0 string audio" Info.plist
fi

Third - Using Fastlane

lane :add_background_modes do
  update_info_plist(
    plist_path: //path to info.plist file,
    block: lambda { |plist|
      existing_modes = plist["UIBackgroundModes"] || []
      plist["UIBackgroundModes"] = existing_modes + ["audio"]
    }
  )
  puts "Added audio and voip keys to Info.plist"
end

Hope this helps. Thanks