pvinis
pvinis

Reputation: 4139

Set react native global variable from Xcode

I saw that there is the __DEV__ variable, and I would like to add my own, say __USE_THIS__. Then way I set this is when the Xcode project is building, but I don't know how to pass that to the js part. Can I use the bundle script and pass it there somehow? When is the __DEV__ set, and how?

Upvotes: 2

Views: 1204

Answers (1)

mien
mien

Reputation: 66

You could have Target/Scheme specific variables exposed to javascript by writing them to a file during Build Phase and then import them in the javascript.

1) Add the following to "Bundle React Native code and images" under "Build Phase" for your target:

export NODE_BINARY=node
echo '{"target": "MyTarget"}' > ../build-info.json
../node_modules/react-native/packager/react-native-xcode.sh

Example screenshot from xcode

2) Import them in the javascript, for example in index.ios.js

import buildInfo from '../../build-info.json'
console.log(buildInfo.target)

It should also be possible to write a file to $PROJECT_ROOT/.env and use it through something like react-native-config instead if you prefer

Upvotes: 5

Related Questions