How to access .env variables in AppDelegate.m from Flutter applications?

Viewed 1786

as instructed from a similar problem Hide Google Maps API key from source control in a Flutter app

AppDelegate.m

#include "AppDelegate.h"
#include "GeneratedPluginRegistrant.h"
#import "GoogleMaps/GoogleMaps.h"

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application
    didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
     NSString* mapsApiKey = [[NSProcessInfo processInfo] environment][@"FLUTTER_GMAPS_API_KEY"];
  [GMSServices provideAPIKey: mapsApiKey];
  [GeneratedPluginRegistrant registerWithRegistry:self];
  return [super application:application didFinishLaunchingWithOptions:launchOptions];
}

@end

If i change provideApiKey to @"theApiKeyImTryingToHide" everything works fine.

3 Answers

I found this question by searching answer for the react-native lib maybe for someone it can be useful.

so, to read variable from env in RN you have to add

NSString *mapsApiKey = [ReactNativeConfig envFor:@"GOOGLE_MAPS_API_KEY"];
[GMSServices provideAPIKey: mapsApiKey];

into top of didFinishLaunchingWithOptions method in AppDelegate.m file and firstly don't forget to import

#import "ReactNativeConfig.h"

ReactNativeConfig provide GOOGLE_MAPS_API_KEY

Related