How to read environment variables inside AppDelegate or AndroidManifest using React Native Config
— React Native, Android, iOS — 1 min read
To read environment variables inside the AppDelegate file in iOS or the AndroidManifest file in Android using React Native Config you need to already have react-native-config installed and properly setup.
I'm using the following .env
file as an example for this tutorial
1MY_API_KEY=123456789
Android
Add a meta-data tag inside application
tag in AndroidManifest.xml:
1<meta-data2 android:name="com.example.API_URL"3 android:value="@string/MY_API_KEY"4/>
iOS
- First, open the AppDelegate.m(m) file and import the React Native Config module before the
@implementation AppDelegate
declaration - Inside
didFinishLaunchingWithOptions
method in AppDelegate.m create aNSString
with the name of your choice and set your environment key name inside the quotes
1// 1. import module2#import "RNCConfig.h"3
4@implementation AppDelegate5
6- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions7
8// 2. declare the variable9NSString *myApiKey = [RNCConfig envFor:@"MY_API_KEY"];10
11// example of use:12[GMSServices provideAPIKey: myApiKey];
Remember to build your app after implementing the changes above.
That's it! Now you can read environment variables inside the AppDelegate or AndroidManifest using React Native Config.