Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

flutter

dart

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.

like image 917
Jonathan Nicholas Avatar asked Oct 22 '25 04:10

Jonathan Nicholas


1 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

like image 146
kuka Avatar answered Oct 26 '25 20:10

kuka