React Native iOS项目Xcode编译报错:FBReactNativeSpec.h文件未找到
解决React Native Xcode构建时找不到FBReactNativeSpec.h的问题
问题描述
使用npx react-native start可正常运行iOS应用,但Xcode构建时出现错误:
/node_modules/react-native/Libraries/Settings/RCTSettingsManager.mm:10:9: fatal error: 'FBReactNativeSpec/FBReactNativeSpec.h' file not found #import <FBReactNativeSpec/FBReactNativeSpec.h>. ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
环境信息:
- macOS 15.0 (Apple M2)
- Node 22.9.0、npm 10.8.3
- CocoaPods 1.15.2
- Xcode 16.0
- React Native 0.75.4
- 项目Podfile配置:
# Resolve react_native_pods.rb with node to allow for hoisting use_frameworks! :linkage => :static $RNFirebaseAsStaticFramework = true require Pod::Executable.execute_command('node', ['-p', 'require.resolve( "react-native/scripts/react_native_pods.rb", {paths: [process.argv[1]]}, )', __dir__]).strip platform :ios, min_ios_version_supported prepare_react_native_project! linkage = ENV['USE_FRAMEWORKS'] if linkage != nil Pod::UI.puts "Configuring Pod with #{linkage}ally linked Frameworks".green use_frameworks! :linkage => linkage.to_sym end target 'Test' do config = use_native_modules! use_react_native!( :path => config[:reactNativePath], # An absolute path to your application root. :app_path => "#{Pod::Config.instance.installation_root}/.." ) pod 'RevenueCat' pod 'RevenueCatUI' target 'TestTests' do inherit! :complete # Pods for testing end post_install do |installer| # https://github.com/facebook/react-native/blob/main/packages/react-native/scripts/react_native_pods.rb#L197-L202 react_native_post_install( installer, config[:reactNativePath], :mac_catalyst_enabled => false, # :ccache_enabled => true ) end end
解决步骤
1. 清理并重新安装Pods
执行以下命令彻底清理Pod相关文件并重新安装:
cd ios rm -rf Pods Podfile.lock pod cache clean --all pod install
2. 修复Podfile中的框架配置冲突
Podfile中两次调用use_frameworks!可能导致框架链接异常,修改Podfile统一静态框架配置:
$RNFirebaseAsStaticFramework = true require Pod::Executable.execute_command('node', ['-p', 'require.resolve( "react-native/scripts/react_native_pods.rb", {paths: [process.argv[1]]}, )', __dir__]).strip platform :ios, min_ios_version_supported prepare_react_native_project! # 统一设置静态框架,移除重复的环境变量判断逻辑 use_frameworks! :linkage => :static target 'Test' do config = use_native_modules! use_react_native!( :path => config[:reactNativePath], :app_path => "#{Pod::Config.instance.installation_root}/.." ) pod 'RevenueCat' pod 'RevenueCatUI' target 'TestTests' do inherit! :complete end post_install do |installer| react_native_post_install( installer, config[:reactNativePath], :mac_catalyst_enabled => false ) end end
修改完成后回到ios目录重新执行pod install。
3. 手动生成React Native Specs
FBReactNativeSpec为自动生成文件,可能存在生成不完整情况,执行以下命令手动生成:
npx react-native generate-specs
生成完成后再次执行cd ios && pod install。
4. 检查Xcode项目配置
- 打开Xcode项目,选中目标Target,进入Build Settings
- 找到Header Search Paths,确保添加以下路径并设置为Recursive:
$(SRCROOT)/Pods/Headers/Public$(SRCROOT)/../node_modules/react-native/Libraries
- 按下
Cmd+Shift+K清理Xcode构建缓存,重启Xcode后重新构建项目。
内容的提问来源于stack exchange,提问作者Daniel Haidari
相关产品推荐
相关产品推荐

