React Native 0.69.2中AppDelegate.mm报FIRApp未声明标识符错误
问题背景
在React Native 0.69.2项目中尝试实现Firebase图片上传功能,但项目无法读取@react-native-firebase/storage,排查后发现AppDelegate中配置FIRApp时出错,报错提示Use of undeclared identifier 'FIRApp'。项目使用新版AppDelegate写法(基于#if RCT_NEW_ARCH_ENABLED而非旧版FB_SONARKIT_ENABLED),现有方案多针对旧版C语言AppDelegate,找不到适配当前C++版AppDelegate的解决办法。已确认Firebase Storage和App组件安装正确。
问题代码片段:
... #import <React/RCTAppSetupUtils.h> #if RCT_NEW_ARCH_ENABLED #import <React/CoreModulesPlugins.h> #import <React/RCTCxxBridgeDelegate.h> #import <React/RCTFabricSurfaceHostingProxyRootView.h> #import <React/RCTSurfacePresenter.h> #import <React/RCTSurfacePresenterBridgeAdapter.h> #import <ReactCommon/RCTTurboModuleManager.h> #import <react/config/ReactNativeConfig.h> #import <Firebase.h> static NSString *const kRNConcurrentRoot = @"concurrentRoot"; @interface AppDelegate () <RCTCxxBridgeDelegate, RCTTurboModuleManagerDelegate> { RCTTurboModuleManager *_turboModuleManager; RCTSurfacePresenterBridgeAdapter *_bridgeAdapter; std::shared_ptr<const facebook::react::ReactNativeConfig> _reactNativeConfig; facebook::react::ContextContainer::Shared _contextContainer; } @end #endif @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { [FIRApp configure]; ///ERROR: Use of undeclared identifier 'FIREAPP' RCTAppSetupPrepareApp(application); ...
核心原因
你的#import <Firebase.h>被包裹在#if RCT_NEW_ARCH_ENABLED条件编译块内,而[FIRApp configure]调用在全局的application:didFinishLaunchingWithOptions:方法中,这会导致:
- 未启用新架构时,条件块内的
#import <Firebase.h>不会被编译,编译器找不到FIRApp的定义; - 即使启用新架构,
application:didFinishLaunchingWithOptions:不在条件块范围内,依然无法识别FIRApp。
解决步骤
1. 调整Firebase头文件导入位置
将#import <Firebase.h>移到条件编译块外,放在文件顶部的公共导入区域:
#import <React/RCTAppSetupUtils.h> #import <Firebase.h> // 移到此处,脱离条件编译块 #if RCT_NEW_ARCH_ENABLED #import <React/CoreModulesPlugins.h> #import <React/RCTCxxBridgeDelegate.h> #import <React/RCTFabricSurfaceHostingProxyRootView.h> #import <React/RCTSurfacePresenter.h> #import <React/RCTSurfacePresenterBridgeAdapter.h> #import <ReactCommon/RCTTurboModuleManager.h> #import <react/config/ReactNativeConfig.h> static NSString *const kRNConcurrentRoot = @"concurrentRoot"; @interface AppDelegate () <RCTCxxBridgeDelegate, RCTTurboModuleManagerDelegate> { RCTTurboModuleManager *_turboModuleManager; RCTSurfacePresenterBridgeAdapter *_bridgeAdapter; std::shared_ptr<const facebook::react::ReactNativeConfig> _reactNativeConfig; facebook::react::ContextContainer::Shared _contextContainer; } @end #endif @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { [FIRApp configure]; // 现在可以正常识别FIRApp RCTAppSetupPrepareApp(application); // ... 剩余代码
2. 确认Pod依赖配置
检查ios目录下的Podfile是否包含Firebase核心和Storage依赖:
pod 'Firebase/Core' pod 'Firebase/Storage'
执行cd ios && pod install确保依赖安装完成。
3. 验证GoogleService-Info.plist配置
确保GoogleService-Info.plist已添加到Xcode项目中,并且在文件的「Target Membership」中勾选了当前项目的target。
额外说明
这个问题和AppDelegate是.mm(C++兼容)无关,纯粹是条件编译的范围错误。只要让Firebase的头文件在全局范围内被导入,就能解决FIRApp未声明的问题。
内容的提问来源于stack exchange,提问作者Anthony Humphreys

