Read configuration values from Info.plist (iOS) or from resources strings.xml (Android)
npm install react-native-config-info
cd ios && pod install
Add your values into your Info.plist (iOS) or strings.xml (Android).
ios/[project]/Info.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>BackendUrl</key>
<string>https://api.example.com</string>
</dict>
</plist>
android/app/src/main/res/values/strings.xml
<resources>
<string name="BackendUrl">https://api.example.com</string>
</resources>
Get your values.
import ConfigInfo from 'react-native-config-info';
async function main() {
await ConfigInfo.get('BackendUrl'); // https://api.example.com
await ConfigInfo.getOrThrow('BackendUrl'); // https://api.example.com
await ConfigInfo.get('UnknownKey'); // null
await ConfigInfo.getOrThrow('UnknownKey'); // [RNConfigInfoException: 'UnknownKey' value is missing from ….]
}
Get your values synchronously.
As said in the official documentation, synchronous methods are not recommended.
Only use them if you have no other option.
import ConfigInfo from 'react-native-config-info';
ConfigInfo.getSync('BackendUrl'); // https://api.example.com
ConfigInfo.getSyncOrThrow('BackendUrl'); // https://api.example.com
ConfigInfo.getSync('UnknownKey'); // null
ConfigInfo.getSyncOrThrow('UnknownKey'); // [RNConfigInfoException: 'UnknownKey' value is missing from ….]
MIT © Rémy Abitbol.