React Native iOS构建失败:FirebaseStorage头文件缺失及RNFBApp错误
问题
执行npx react-native run-ios构建React Native项目时持续触发Error 65,核心报错:
fatal error: 'FirebaseStorage/FirebaseStorage-Swift.h' file not found (in target 'RNFBApp' from project 'Pods')
- 仅运行
npx react-native start且代码未引入Firebase Storage库时,应用可正常启动;一旦添加import storage from "@react-native-firebase/storage";,会出现AppRegistry相关错误,判断为构建失败的衍生问题。 - 已按Firebase官方指南完成依赖安装:
sudo npm i @react-native-firebase/app sudo npm i @react-native-firebase/storage - 确定问题出在iOS目录,怀疑Podfile配置有误,尝试过GitHub和Stack Overflow的公开方案均无效。
当前Podfile内容
require_relative '../node_modules/react-native/scripts/react_native_pods' require_relative '../node_modules/@react-native-community/cli-platform-ios/native_modules' $RNFirebaseAsStaticFramework = true platform :ios, '12.4' install! 'cocoapods', :deterministic_uuids => false use_frameworks! target 'frontendMobile' do config = use_native_modules! # Flags change depending on the env values. flags = get_default_flags() pod 'Firebase', :modular_headers => true pod 'FirebaseCoreInternal', :modular_headers => true pod 'GoogleUtilities', :modular_headers => true pod 'FirebaseStorage', :modular_headers => true pod 'FirebaseStorageInternal', :modular_headers => true pod 'FirebaseAppCheckInterop', :modular_headers => true pod 'FirebaseAuthInterop', :modular_headers => true pod 'FirebaseCore', :modular_headers => true pod 'FirebaseCoreExtension', :modular_headers => true use_react_native!( :path => config[:reactNativePath], # to enable hermes on iOS, change `false` to `true` and then install pods :hermes_enabled => flags[:hermes_enabled], :fabric_enabled => flags[:fabric_enabled], # An absolute path to your application root. :app_path => "#{Pod::Config.instance.installation_root}/.." ) target 'frontendMobileTests' do inherit! :complete # Pods for testing end # Enables Flipper. # # Note that if you have use_frameworks! enabled, Flipper will not work and # you should disable the next line. use_flipper!() post_install do |installer| react_native_post_install(installer) __apply_Xcode_12_5_M1_post_install_workaround(installer) if ['RNFBApp', 'RNFBMessaging'].include?(target.name) target.add_system_framework(installer.pods_project.targets.find{|target| target.name == 'GoogleUtilities'}) end if ['RNFBCrashlytics'].include?(target.name) target.add_system_framework(installer.pods_project.targets.find{|target| target.name == 'Fabric'}) target.add_system_framework(installer.pods_project.targets.find{|target| target.name == 'Crashlytics'}) end end end
当前AppDelegate.mm关键内容
#import "AppDelegate.h" #import <React/RCTBridge.h> #import <React/RCTBundleURLProvider.h> #import <React/RCTRootView.h> #import <React/RCTAppSetupUtils.h> #import <Firebase.h> #if RCT_NEW_ARCH_ENABLED // 省略新架构相关代码 #endif @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { [FIRApp configure]; RCTAppSetupPrepareApp(application); RCTBridge *bridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:launchOptions]; #if RCT_NEW_ARCH_ENABLED // 省略新架构相关代码 #endif
解决思路
1. 修正框架链接模式
Podfile中use_frameworks!默认是动态框架,和$RNFirebaseAsStaticFramework = true存在冲突,替换为静态框架模式:
use_frameworks! :linkage => :static
静态框架能更好兼容React Native Firebase的静态库配置,避免Swift头文件找不到的问题。
2. 精简Firebase依赖声明
不需要手动逐个添加Firebase子库,React Native Firebase会自动关联所需依赖,删除Podfile中手动添加的所有Firebase相关pod行,改为仅在use_native_modules!后添加:
# 为Firebase核心依赖开启模块化头文件 $RNFirebaseAsStaticFramework = true pod 'FirebaseCore', :modular_headers => true pod 'GoogleUtilities', :modular_headers => true
3. 彻底清理并重新安装Pods
执行以下命令清理缓存后重装:
cd ios rm -rf Pods Podfile.lock pod deintegrate pod repo update pod install
4. 检查Xcode项目配置
- 打开Xcode,进入项目
Build Settings,搜索Header Search Paths,确保包含$(inherited)和${PODS_ROOT}/FirebaseStorage/Sources - 确认项目的
iOS Deployment Target与Podfile中platform :ios, '12.4'完全一致
5. 修复Post Install脚本错误
原脚本中target.name的判断逻辑有误,因为post_install块内的target并非当前目标,改为遍历所有目标:
post_install do |installer| react_native_post_install(installer) __apply_Xcode_12_5_M1_post_install_workaround(installer) installer.pods_project.targets.each do |target| if ['RNFBApp', 'RNFBMessaging'].include?(target.name) google_utils_target = installer.pods_project.targets.find { |t| t.name == 'GoogleUtilities' } target.add_dependency(google_utils_target) if google_utils_target end if ['RNFBCrashlytics'].include?(target.name) fabric_target = installer.pods_project.targets.find { |t| t.name == 'Fabric' } crashlytics_target = installer.pods_project.targets.find { |t| t.name == 'Crashlytics' } target.add_dependency(fabric_target) if fabric_target target.add_dependency(crashlytics_target) if crashlytics_target end end end
内容的提问来源于stack exchange,提问作者Anthony Humphreys
相关产品推荐
相关产品推荐

