lugg / react-native-config

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

Conflicting instructions & description between root README.md and Example/README.md #131

Open PatNeedham opened 7 years ago

PatNeedham commented 7 years ago

I am trying to determine why the react-native-config module is working fine on the Android side of my app (retrieving correct env variables from either .env.dev. or .env.prod) but failing to do so when running on iOS. When logging to the console, all values of Config.VARIABLE_NAME show up as undefined.

I thought starting from the official example and comparing to see what I'm doing wrong would be a good start. but the iOS instructions from the root directory readme do not match up with the Example/ios/Example.xcodeproj project file when verifying the Running on iOS section:

Notice there are two schemes setup for the application: "Example" runs the default build, reading vars from .env – and "Example (prod)" is configured to read from .env.prod.

That was not the case for me, as when I opened Example/ios/Example.xcodeproj after cloning the repo, the "Example (prod)" scheme was nowhere to be found. Additionally, running ENVFILE=.env.prod react-native run-ios and react-native run-ios both resulted in the app running with API_URL=localhost as the center text even though in .env.prod it's defined as https://myapi.com.

However, when I edited the Example scheme within Xcode to add echo ".env.prod" > /tmp/envfile as a Pre-action run script, https://myapi.com DID appear on the app, regardless of whether I preceeded run-ios with ENVFILE=.env.prod or ENVFILE=.env.

That leads me to ask this: is the extra ENVFILE=_envfile only meant to be used with react-native run-android and not with the iOS equivalent? That led me to wonder if echo ".env.prod" > /tmp/envfile could somehow be changed to echo "${ENVFILE}" > /tmp/envfile. And it worked! Running ENVFILE=.env react-native run-ios and ENVFILE=.env.prod react-native run-ios now give the expected results, with env variables being retrieved from the specified file.

Hopefully this issue and my workaround can help others who are having trouble getting react-native-config to work properly on iOS.

andersryanc commented 7 years ago

The suggestion to add echo "${ENVFILE}" > /tmp/envfile as a run script in the build scheme seems to have worked great... however, it only seems to be working for me if I build/run from the command line. If I try to run a build inside Xcode, the ENVFILE var is not defined and thus errors and stops the build process.

Any suggestion on how to fix this? Or how to run builds for production and/or to my physical device?

wswebcreation commented 6 years ago

@PatNeedham and @andersryanc

Did you get this working?

I've got it working now by adding this in XCODE in:

if [ "${ENVFILE}" ]; then echo "${ENVFILE}" > /tmp/envfile ; else echo ".env" > /tmp/envfile; fi

Now I can also use

# with an ENVFILE
ENVFILE=.env.prod react-native run-ios

# without, just the default
react-native run-ios

the same as on Android

andersryanc commented 6 years ago

I ended up not needing this package.

wswebcreation commented 6 years ago

@andersryanc

Did you use something else then?

andersryanc commented 6 years ago

No, I just realized I didn't need a config package in general. I think I may have even removed another dependency I was trying to use this with.