首页 > 解决方案 > 在 react-native swift 中使用环境变量

问题描述

我正在开发 React-Native App 并希望配置可以与 iOS 应用程序和 Android 共享的环境变量。

文档要求我在 AppDelegate.m 中添加这一行

[GMSServices provideAPIKey:@"Azzz8"];`

Azzz8将是我的 api 密钥。现在我想将它传递到环境文件中并从那里传递(到 ios 和 android)。知道我该怎么做吗?

这就是我的 AppDelegate.m 代码的样子

/**
 * Copyright (c) Facebook, Inc. and its affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */

#import "AppDelegate.h"
#import <Firebase.h>
#import <React/RCTBridge.h>
#import <React/RCTBundleURLProvider.h>
#import <React/RCTRootView.h>
#import <React/RCTLinkingManager.h>
#import <UMCore/UMModuleRegistry.h>
#import <UMReactNativeAdapter/UMNativeModulesProxy.h>
#import <UMReactNativeAdapter/UMModuleRegistryAdapter.h>
#import <GoogleMaps/GoogleMaps.h>

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  /* Firebasee App Config */
  [FIRApp configure];
  [GMSServices provideAPIKey:@"1234"];

标签: iosswiftreact-native

解决方案


看看react-native-config库。它允许您访问在本机部分的.env文件中定义的环境变量,以及应用程序的 JavaScript 部分。


推荐阅读