React-native-config: Cannot read env variable in info.plist

Created on 27 Mar 2020  路  10Comments  路  Source: luggit/react-native-config

I have follow the doc until:
Availability in Build settings and Info.plist
step 6. create new build phase for the scheme which will generate "tmp.xcconfig" before each build exposing values to Build Settings and Info.plist (this snippet has to be placed after "echo ... > tmp/envfile" if approach explained below is used)

  • I have created schema for my app then in build -> pre-actions. I click add new script and paste the following:
    echo ".env.staging" > /tmp/envfile
    "${SRCROOT}/../node_modules/react-native-config/ios/ReactNativeConfig/BuildXCConfig.rb" "${SRCROOT}/.." "${SRCROOT}/tmp.xcconfig"
  • then I go to info.plist and use $(MYENVVAR) to get value from .env file but it could not get any value.

Has anyone ever faced it?

Happy to see any comments

Most helpful comment

I ran into the same issue and fixed it by referencing the correct env variable name.

It used to be: __RN_CONFIG_XXX
Correct one: RNC_XXX

For more details, have a look at the generating script:

New way: https://github.com/luggit/react-native-config/blob/master/ios/ReactNativeConfig/BuildDotenvConfig.rb#L29
Old way: https://github.com/luggit/react-native-config/blob/v0.11.7/ios/ReactNativeConfig/BuildDotenvConfig.ruby#L69

All 10 comments

@MRSEREY this comment helped me.

Also, be sure to clean your build before running and testing it.

@germanp173 thanks, but It still does not work.
in build settings -> packaging I change

  • Info.plist Other Preprocessor Flags to -traditional
  • Info.plist Preprocessor Prefix File to build/GeneratedInfoPlistDotEnv.h
  • Preprocess Info.plist File to yes
  • remove postinstall script specific for react-native-config in Podfile
  • pod install
  • clean profile and build

I got an error build/GeneratedInfoPlistDotEnv.h not found

@MRSEREY sorry I should have been more clear and specified that the comment helped me by instructing me to remove those old settings which you just laid out (make sure you read the comment carefully). After I removed those Preprocessor flags and cleaned the project, it worked for me.

If you didn't have any of those old settings in place then:

  1. When you open ${SRCROOT}/tmp.xcconfig after your build, do you see your environment variables there?

@germanp173 it didn't see even tmp.xcconfig in ios folder

@germanp173 I can see tmp.xcconfig file now which contain my env there. But I still cannot access it in Info.plist.
I use this to access env $(MY_VAR)

hi @MRSEREY , do you resolve this issue? Thanks

@MRSEREY have you enabled "Provide build settings from " ?

I ran into the same issue and fixed it by referencing the correct env variable name.

It used to be: __RN_CONFIG_XXX
Correct one: RNC_XXX

For more details, have a look at the generating script:

New way: https://github.com/luggit/react-native-config/blob/master/ios/ReactNativeConfig/BuildDotenvConfig.rb#L29
Old way: https://github.com/luggit/react-native-config/blob/v0.11.7/ios/ReactNativeConfig/BuildDotenvConfig.ruby#L69

I ran into the same issue and fixed it by referencing the correct env variable name.

It used to be: __RN_CONFIG_XXX
Correct one: RNC_XXX

For more details, have a look at the generating script:

New way: https://github.com/luggit/react-native-config/blob/master/ios/ReactNativeConfig/BuildDotenvConfig.rb#L29
Old way: https://github.com/luggit/react-native-config/blob/v0.11.7/ios/ReactNativeConfig/BuildDotenvConfig.ruby#L69
I was having the same issue. This worked for me Thanks!

For me, there were two problems:

  1. If you have syntax errors in your .env file, the process fails without throwing any env-file-specific errors. In my case, I removed all comments and empty lines, and switched all double-quotes for single-quotes. Not sure which of these was the syntax error, but that fixed it.

  2. Forgot to add Config.xcconfig to target membership.

Worked perfectly after that 馃憤

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rpereira picture rpereira  路  3Comments

swl367 picture swl367  路  4Comments

NachtRitter picture NachtRitter  路  4Comments

sonlexqt picture sonlexqt  路  4Comments

himanshu-satija picture himanshu-satija  路  3Comments