如何将 react-native-config 与使用 Swift 构建的 React Native 应用程序一起使用

问题描述

我正在尝试将 react-native-config 与用 Swift 配置的 react-native 结合使用。

按照使用指南,它告诉我导入 #import "ReactNativeConfig.h"

我在桥接头中导入了它,但找不到它。

有谁知道如何将这个库与用 Swift 构建的 React Native 应用程序一起使用?

解决方法

您只需要在桥接头中添加 #import "ReactNativeConfig.h"。如果你这样做了,你就可以在你的 swift 代码中使用它而无需任何额外的导入。

这里有一些我觉得很有帮助的文章。 https://medium.com/@subhangdxt/bridging-objective-c-and-swift-classes-5cb4139d9c80

这是我的代码:

我的应用程序-Bridging-Header.h:

...
#import "ReactNativeConfig.h"
...

我的 AppDelegate.swift

func application(_ application: UIApplication,didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

  ...

  if let gmsApiKey = ReactNativeConfig.env(for: "GOOGLE_MAPS_API_KEY") {
    GMSServices.provideAPIKey(gmsApiKey)
  } else {
    print("ERROR: No goole maps api key provided!")
  }

  bridge = RCTBridge(delegate: self,launchOptions: launchOptions)

  ...

  return true
}