How to set different env file with different bundle identifier?

908 Views Asked by At

I use react-native-config with XCode setting.

I want to have three version are

myapp

Prod

myapp.Dev

Dev

myapp.Qa

Qa

Here is my step:

  1. Adding new configurations Dev and Qa with XCode
  2. Adding new schemes for Dev and Qa with XCode
  3. set User-Defined for Dev and Qa with different bundle identifier

I run the project with XCode, It works, I can see three apps on simulator (Dev Qa Prod)

then I want to get variant from different file, so I set three env file

.env

API_URL=https://myapi.com

.env.dev

API_URL=https://myapi.dev.com

.env.qa

API_URL=https://myapi.qa.com

I also set the Run Script with XCode, for instance myapp.Dev is cp "${PROJECT_DIR}/../.env.dev" "${PROJECT_DIR}/../.env"

I have two problems:

  1. If run the project myapp.Dev with XCode

get variable from .env (wrong) bundle identifier is Dev (correct)

  1. If I run the command ENVFILE=.env.dev npx react-native run-ios to build the project,

get variable from .env file (wrong) and variable becomes to https://myapi.dev.com, bundle identifier is Prod (wrong)

I have no idea what step is wrong, how do I just run mypp.Dev and read from .env.Dev file ?

2

There are 2 best solutions below

1
On

As my concern, I'm using this library :

react-native-config

0
On

Check this.

It seems Xcode can only cope with one target at a time. It kinda caches the last target running. So if you build more than one at a time, you may find it keeps on reading the last .env.target file used. Therefore: Build only one instance from Xcode. Build other/all instances of your app (schemes) from the console like so:

npm run ios -- --scheme YourAppScheme