iOS 老项目适配 #Preview 预览功能

时间:2025-02-06 14:27:56

前言

iOS 开发者 最憋屈的就是UI 布局慢,一直以来没有实时预览功能,虽然swiftUI 早就支持了,但是目前主流还是使用UIKit在布局,iOS 17 苹果推出了 #Preview 可以支持UIKit 实时预览,但是仅仅是 iOS 17,老项目怎么办呢?于是就有了这篇 老项目适配 #Preview 预览 的文章,从新建 Targets 是正文开始,前面的是我在探索过程中的踩坑记录。

环境
MacOS: 15.1
Apple: M4
Xcode: 16.1

一、AlipaySDK 报错

错误日志
PREVIEW UPDATE ERROR:
FailedToLaunchAppError: Failed to launch com.xxx.xxx
XOJITError: x86_64 slice of /Users/xxx/Documents/YLProject/poetry/Pods/AlipaySDK-iOS/AlipaySDK.framework/AlipaySDK does not contain an archive

解决

  1. 更新 Podfile 配置:
post_install do |installer|
    installer.pods_project.targets.each do |target|
        target.build_configurations.each do |config|
            # Needed for building for simulator on M1 Macs
           config.build_settings['ONLY_ACTIVE_ARCH'] = 'NO'
           config.build_settings['EXCLUDED_ARCHS[sdk=iphonesimulator*]'] = 'arm64'
        end
    end
end
  1. 清理并重新安装依赖
pod deintegrate
pod install

二、ProjectWidgetExtension 小组件中报错

错误日志
Could not find module ‘WebPKit’ for target ‘arm64-apple-ios-simulator’; found: x86_64-apple-ios-simulator, at: /Users/liyanhong/Library/Developer/Xcode/DerivedData/poetry-fgnfhoqfwdsbnpaudqkcloqdigxt/Index.noindex/Build/Products/Debug-iphonesimulator/WebPKit/WebPKit.framework/Modules/WebPKit.swiftmodule

解决
在 ProjectWidgetExtension 的 Build Settings 中,手动排除模拟器架构:
1. 打开 Build Settings。
2. 搜索 Excluded Architectures。
3. 在 Any iOS Simulator SDK 中添加 arm64。