lugg / react-native-config

Bring some 12 factor love to your mobile apps!
MIT License
4.83k stars 656 forks source link

[iOS] envfiles set in the podfile are not working #618

Open rawatnaresh opened 3 years ago

rawatnaresh commented 3 years ago

As mentioned in the docs we can use following approach to use different env file for different build configurations

Alternatively, if you have separated build configurations, you may easily set the different envfiles per configuration by adding these lines into the end of Podfile

Based on that, this is how my Podfile looks like

require_relative '../node_modules/react-native/scripts/react_native_pods'
require_relative '../node_modules/@react-native-community/cli-platform-ios/native_modules'

platform :ios, '12.1'

target 'unicruise' do
  config = use_native_modules!

  use_react_native!(
    :path => config[:reactNativePath],
    # to enable hermes on iOS, change `false` to `true` and then install pods
    :hermes_enabled => false
  )

  pod 'RNSquareInAppPayments', :path => '../node_modules/react-native-square-in-app-payments'

  target 'unicruiseTests' do
    inherit! :complete
    # Pods for testing
  end

  use_flipper!()

  rn_maps_path = '../node_modules/react-native-maps'
  pod 'react-native-google-maps', :path => rn_maps_path
  pod 'GoogleMaps'
  pod 'Google-Maps-iOS-Utils'
  pod 'GoogleSignIn', '~> 5.0.2'

  ENVFILES = {
    'Debug' => '$(PODS_ROOT)/../../.env',
    'Staging' => '$(PODS_ROOT)/../../.env',
    'Release' => '$(PODS_ROOT)/../../.env.prod',
  }

  post_install do |installer|
    react_native_post_install(installer)
    installer.pods_project.targets.each do |target|
      target.build_configurations.each do |config|
        if target.name == 'react-native-config'
          config.build_settings['ENVFILE'] = ENVFILES[config.name]
        end
      end
    end
  end

end

I've .env and .env.prod in the root dir of my project and I'm trying to use .env for both Debug & Staging and .env.prod for Release

Here I'm using Release configuration for Release Schema

Screen Shot 2021-10-21 at 09 30 22

When i select the Release schema and run the app it is still reading values fro .env file. Am i missing something here?

ibnukipa commented 3 years ago

One thing @rawatnaresh , you do need use pre-actions since you use Podfile configuration. However I have the same problem, the configuration in Podfile somehow doesn't work.

react-native: 0.66.1 react-native-config: 1.4.5

rawatnaresh commented 3 years ago

@ibnukipa so there is no other option other than using it like this in pre-actions

cp "${PROJECT_DIR}/../.env.staging" "${PROJECT_DIR}/../.env"  # replace .env.staging for your file
rawatnaresh commented 3 years ago

@luancurti sorry to bother you but do you have any suggestion on how can i make this work.

StanSarr commented 2 years ago

Did you guys find any way to make it work? @rawatnaresh @ibnukipa

rawatnaresh commented 2 years ago

@StanSarr Either you can add this in build phases

if [ "${CONFIGURATION}" == "Release" ]
then
    echo ".env.production" > /tmp/envfile
else
    echo ".env.staging" > /tmp/envfile
fi

OR paste this into Schema->Edit Schema->Pre-built actions

 echo ".env.staging" > /tmp/envfile
herarya commented 2 years ago
echo ".env.staging" > /tmp/envfile

thanks, it's work

sanderdewilde commented 2 years ago

I've got it to work by using

ENVFILES = {
  'Debug' => '$(PODS_ROOT)/../../../.env.development',
  'Release' => '$(PODS_ROOT)/../../../.env.production',
}

instead of

ENVFILES = {
  'Debug' => '$(PODS_ROOT)/../../.env.development',
  'Release' => '$(PODS_ROOT)/../../.env.production',
}

Don't know why it works though. I would think PODS_ROOT is ios/Pods/ so going two directory levels up would suffice, but apparently that's not the case.

richlewis14 commented 2 years ago

Hi, I am facing similar issues, was wondering how to go about confirming that the env vars have been written/added to the build? @sanderdewilde @herarya was wondering if you had any ideas please, or anyone else in this thread please, thank you

verycosy commented 2 years ago

@richlewis14 Hi! Did you try rm -rf ios/Pods ? I also can not apply multiple env files for project. It seems like not reflected Podfile. So I just remove Pods file, npx pod-install && yarn ios, and it works!

PaperMonster commented 2 months ago

Pre-action script that works for me

if [ "${CONFIGURATION}" = "Debug" ]; then
    echo ".env.development" > /tmp/envfile
elif [ "${CONFIGURATION}" = "Staging" ]; then
    echo ".env.staging" > /tmp/envfile
elif [ "${CONFIGURATION}" = "Release" ]; then
    echo ".env.production" > /tmp/envfile
fi

"${SRCROOT}/../node_modules/react-native-config/ios/ReactNativeConfig/BuildXCConfig.rb" "${SRCROOT}/.." "${SRCROOT}/tmp.xcconfig"

The postinstall script in Podfile adds build setting named ENVFILE to react-native-config target, but BuildXCConfig.rb and ReadDotEnv.rb scripts don't read build settings at all, let alone the build settings in react-native-config target. Since ReadDotEnv.rb always picks /tmp/envfile first, let pre-action prepare right value of /tmp/envfile for BuildXCConfig.rb to consume. Then this postinstall script in Podfile can be removed.