无法将Swift Framework接入Expo原生模块:编译器找不到头文件
Expo原生模块集成预编译Swift框架的头文件找不到问题
问题描述
我开发的Expo应用需要和蓝牙设备通信,设备厂商提供了无源码的预编译Swift框架CustomBluetoothModule.framework,框架包含伞头文件及相关头文件。我编写Expo原生模块封装该框架,以便在React Native代码中调用,但集成后编译报错,提示找不到框架的头文件。
编译错误日志
› Executing expo-web-browser Pods/ExpoWebBrowser » Copy generated compatibility header › Executing native-module-test Pods/TestLocalModule » Copy generated compatibility header › Compiling Pods/Pods-nativemoduletest » Pods-nativemoduletest-dummy.m › Packaging Pods/Pods-nativemoduletest » libPods-nativemoduletest.a › Executing nativemoduletest » [CP] Check Pods Manifest.lock › Executing nativemoduletest » [Expo] Configure project › Copying ./PrivacyInfo.xcprivacy ➜ ios/nativemoduletest/PrivacyInfo.xcprivacy › Copying ios/nativemoduletest/Supporting/Expo.plist ➜ ./Expo.plist › Compiling nativemoduletest » SplashScreen.storyboard ❌ (/Users/pubudu/Library/Developer/Xcode/DerivedData/nativemoduletest-denbynqwuqfmebgdbtkenakcxmew/Build/Products/Debug-iphoneos/TestLocalModule/TestLocalModule-umbrella.h:13:9) 11 | #endif 12 | > 13 | #import "CustomBluetoothModule-umbrella.h" | ^ 'CustomBluetoothModule-umbrella.h' file not found 14 | #import "CustomBluetoothModuleDevice.h" 15 | 16 | FOUNDATION_EXPORT double TestLocalModuleVersionNumber; ❌ (/Users/pubudu/Library/Developer/Xcode/DerivedData/nativemoduletest-denbynqwuqfmebgdbtkenakcxmew/Build/Products/Debug-iphoneos/TestLocalModule/TestLocalModule-umbrella.h:13:9) 11 | #endif 12 | > 13 | #import "CustomBluetoothModule-umbrella.h" | ^ 'CustomBluetoothModule-umbrella.h' file not found 14 | #import "CustomBluetoothModuleDevice.h" 15 | 16 | FOUNDATION_EXPORT double TestLocalModuleVersionNumber;
项目结构
- 原生模块目录下包含:
CustomBluetoothModule.framework、TestLocalModule.swift、TestLocalModule.podspec等文件 - 框架内部包含伞头文件
CustomBluetoothModule-umbrella.h及CustomBluetoothModuleDevice.h
框架伞头文件内容
#import "CustomBluetoothModuleDevice.h"
当前Podspec配置
Pod::Spec.new do |s| s.name = 'TestLocalModule' s.version = '1.0.0' s.summary = 'A sample project summary' s.description = 'A sample project description' s.author = '' s.homepage = 'https://docs.expo.dev/modules/' s.platforms = { :ios => '15.1', :tvos => '15.1' } s.source = { git: '' } s.static_framework = true s.dependency 'ExpoModulesCore' # Swift/Objective-C compatibility s.pod_target_xcconfig = { 'DEFINES_MODULE' => 'YES', } s.source_files = "**/*.{h,m,mm,swift,hpp,cpp}" s.vendored_frameworks = 'CustomBluetoothModule.framework' end
解决方法
1. 配置头文件搜索路径
修改Podspec中的pod_target_xcconfig,添加框架头文件的搜索路径,让Xcode能定位到框架内的头文件:
s.pod_target_xcconfig = { 'DEFINES_MODULE' => 'YES', 'HEADER_SEARCH_PATHS' => '"$(PODS_TARGET_SRCROOT)/CustomBluetoothModule.framework/Headers"' }
2. 改用模块式导入
在原生模块代码中,替换直接导入头文件的方式,改用框架模块导入:
- Objective-C代码:
@import CustomBluetoothModule; - Swift代码:
import CustomBluetoothModule
3. 确认框架路径正确性
确保CustomBluetoothModule.framework的位置和Podspec中vendored_frameworks配置的路径一致。如果框架放在子目录(比如ios文件夹),需要更新路径:
s.vendored_frameworks = 'ios/CustomBluetoothModule.framework'
4. 清理缓存重新构建
执行以下命令清理项目缓存后重新编译:
npx expo prebuild --clean npx expo run:ios --device
内容的提问来源于stack exchange,提问作者THpubs
相关产品推荐
相关产品推荐

