如何访问位于 .env 中的环境变量 - React Native

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

我目前有 .env 文件,我正在其中保存 api 键等(在下面的示例中不是实际键):

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

我需要在我的 AppDelegate.m 文件中访问这些密钥。

我试过以下方法:

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

但它没有 work.I 出现错误: "***+[NSString stringWithUTF8String:] NULL cString" .

我无法将变量用作 @"AppID" 我想传入的地方。任何关于如何解决此问题的想法都会有所帮助。

使用"Process.env.{{variable_name}}"访问环境变量。

我最终为此使用了一个库,名为 react-native-config:

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

而且效果非常好。它还有一个系统可以避免延迟(基于状态)缓存,因此一旦您保存 .env 文件,就没有机会记住 .env 文件的先前状态。