Search code examples
react-nativeenvironment-variablesintercomintercom-ios

How to access environment variables located in .env - React Native


I currently have .env file in which I am saving api keys and such (not actual keys in the example below):

API_URL=https://www.example.si/3000
IOS_API_KEY=738d7hfu3hffsjfsd
STRIPE_KEY=pk_fh843f483ff3f43f34
APP_ID=pk_fh843f483ff3f43f34

I need to access these keys in my AppDelegate.m file.

I've tried the following:

NSString *AppID = @(getenv("APP_ID"));

But it didn't work.I get an error : "***+[NSString stringWithUTF8String:] NULL cString" .

I cannot use variable as @"AppID" where I want to pass it in. Any ideas on how to solve this issue would be helpful.


Solution

  • I ended up using a library for this, called react-native-config:

    https://github.com/luggit/react-native-config

    and it works really well. It also has a system in place to avoid delayed (state based) caching so as soon as you save .env file there is no chance of remembering the previous state of .env file.