使用 XCFramework

使用 max xcodebuild 查看 xcodebuild 的使用方法

man xcodebuild

目前 xcodebuild 支持的平台:

  • macOS
  • iOS
  • iOS Simulator
  • watchOS
  • watchOS Simulator
  • tvOS
  • tvOS Simulator

对应的 sdk:

  • macosx
  • iphoneos
  • iphonesimulator
  • watchos
  • watchsimulator
  • appletvos
  • appletvsimulator

具体使用:
关于构建 release 版本, 使用 archive,
BUILD_LIBRARIES_FOR_DISTRIBUTION=YES, 代表构建向后兼容的 framework,
SKIP_INSTALL=NO不直接安装, archive 必须设置

// ios release
xcodebuild archive \
-scheme YourSchemeName \
-destination="iOS" -sdk iphoneos \                  
SKIP_INSTALL=NO \
BUILD_LIBRARIES_FOR_DISTRIBUTION=YES

// ios simulator release
xcodebuild archive \
-scheme YourSchemeName \
-destination="iOS Simulator"  -sdk iphonesimulator \
SKIP_INSTALL=NO \
BUILD_LIBRARIES_FOR_DISTRIBUTION=YES

构建了两个版本的 framework, 本来想使用 lipo 来合并, 但发现会报两个都是 arm64字符集, 不可以合并, 只能使用苹果的新方式, XCFramework

xcodebuild -create-xcframework \
-framework /iosPath/xxx.framework  \
-framework /simulatorPath/xxx.framework \
-output /outputPath/xxx.xcframework

build framework 这项工作可以在 xcode 中进行:

  1. Build Settings >> Build Libraries for Distribution, 设置这个选项为 Yes,
    否则 framework 中不会包含 .swiftmodule 文件, 则不支持向后兼容
  2. 和之前 build .framework的步骤一样
  3. 使用 xcodebuild -create-xcframework 进行多个 framework 的合并, 得到最后的.xcframework

通过上面两种方式 build出来的 framework 都在 Archives: 对应的文件夹下
还可以,
使用 -archivePath 来指定 archive 的文件夹, 或者使用 Xcode, 使用自定义Archives文件夹, 对应 Archive 产生的数据
使用 -derivedDataPath 来指定 DerivedData 的文件夹, 或者使用 Xcode, 使用自定义DerivedData文件夹, 对应 Build For Runnning 产生的数据

Xcode 进行 build 如果使用 Products 文件夹下的 framework 要把 Scheme >> Run 改成 release, 否则在 Build For Runnning 产生的是 Debug framework.

reference: Xcode Help
reference: Binary Frameworks in Swift

你可能感兴趣的:(使用 XCFramework)