Xcode中如何为模拟器排除仅支持真机的Framework
解决仅支持真机的Framework在iOS模拟器上运行的配置方案
你的问题根源在于目标Framework仅包含真机arm64-apple-ios架构,而Apple Silicon模拟器需要arm64-apple-ios-simulator架构,Intel模拟器则依赖x86_64架构,导致编译时匹配不到对应架构的模块。以下是具体的配置解决步骤:
1. 工程层面排除模拟器的arm64架构
- 打开Xcode,选中主工程Target,切换到
Build Settings标签 - 搜索
Excluded Architectures,展开选项:- 在
Debug配置下,为Any iOS Simulator SDK添加arm64(模拟器编译时会跳过arm64架构,Intel模拟器直接用x86_64,Apple Silicon模拟器通过Rosetta转译运行) - 注意:不要在Release配置中添加该排除项,避免影响真机打包的架构完整性
- 在
2. 单独配置Framework的支持平台
如果第一步无效,需要修改Framework本身的配置:
- 在
Build Phases->Link Binary With Libraries中找到xxxxSDK.framework,右键选择Show in Finder - 右键点击Framework文件,选择
Show Package Contents,打开Info.plist - 添加/修改
CFBundleSupportedPlatforms数组,加入iPhoneOS和iPhoneSimulator两个字符串项 - 若Xcode中能看到
Valid Architectures选项,确保其包含arm64和x86_64;若该选项隐藏,可通过右键Add User-Defined Setting手动添加
3. 代码层面条件编译隔离SDK引用
如果前两步仍无法解决,可通过代码条件编译跳过模拟器对SDK的引用:
- 在所有引用
xxxxSDK的代码文件顶部添加:
#if !targetEnvironment(simulator) import xxxxSDK #endif
- 所有调用该SDK的业务代码,都需要包裹在
#if !targetEnvironment(simulator)和#endif之间,确保模拟器编译时不会加载该SDK
4. CocoaPods管理场景的额外配置
若该Framework通过CocoaPods引入,修改Podfile添加以下配置:
post_install do |installer| installer.pods_project.targets.each do |target| if target.name == 'xxxxSDK' target.build_configurations.each do |config| config.build_settings['EXCLUDED_ARCHS[sdk=iphonesimulator*]'] = 'arm64' config.build_settings['VALID_ARCHS'] = 'arm64 x86_64' end end end end
执行pod install更新配置即可
内容的提问来源于stack exchange,提问作者OuSS
相关产品推荐
相关产品推荐

