How to read environment variables inside AppDelegate or AndroidManifest using React Native Config
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
MY_API_KEY=123456789
Android
Add a meta-data tag inside application
tag in AndroidManifest.xml:
<meta-data android:name="com.example.API_URL" android:value="@string/MY_API_KEY" />
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. import module
#import "RNCConfig.h"
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
// 2. declare the variable
NSString *myApiKey = [RNCConfig envFor:@"MY_API_KEY"];
// example of use:
[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.