0

我正在尝试构建通用的 swift 框架。当我按照几个教程完成开发框架时,例如

https://medium.com/swiftindia/build-a-custom-universal-framework-on-ios-swift-549c084de7c8

使用 Xcode 8 创建通用框架?

也可以使用聚合目标,但无法在模拟器中运行。

注意:我使用 Swift 3 开发,Xcode 版本 8.3

我也尝试过使用教程脚本,但失败了。

我的框架 Xcode 扩展也是xcodeproj

我的问题是如何开发通用 swift 框架,该框架将发布给客户端,他可以通过模拟器和真实设备进行测试?

提前致谢。

编辑:我按照这个答案说明并成功构建了 swift 框架。

https://stackoverflow.com/a/26691080/614154

4

1 回答 1

3

您还可以使用以下脚本。也支持 Swift 和 Simulator。

  1. 只需在 Project Settings -> Build Phase 下添加内容作为新的 Run Script Phase
  2. 构建项目
  3. 文件夹将打开,显示您的压缩框架

https://gist.github.com/PaulEhrhardt/6d2be145c5d1b51c216914e7f032013e

#!/bin/sh

# iOS universal library build script supporting swift modules inclusive simulator slices

# prevention from running xcodebuild in a recusive way
if [ "true" == ${ALREADYINVOKED:-false} ]; then
    echo "RECURSION: Detected, stopping"
else
    export ALREADYINVOKED="true"

# output directory for universal framework
UNIVERSAL_OUTPUTFOLDER=${BUILD_DIR}/${CONFIGURATION}-universal
mkdir -p "${UNIVERSAL_OUTPUTFOLDER}/iOS"

# build both device and simulator versions for iOS
xcodebuild -project "${PROJECT_NAME}.xcodeproj" -scheme "${PROJECT_NAME}"  -sdk iphonesimulator -destination 'platform=iOS Simulator,name=iPhone 8' ONLY_ACTIVE_ARCH=NO clean build
xcodebuild -project "${PROJECT_NAME}.xcodeproj" -scheme "${PROJECT_NAME}" -sdk iphoneos ONLY_ACTIVE_ARCH=NO clean build

# copy the framework structure from iphoneos build to the universal folder
cp -R "${BUILD_DIR}/${CONFIGURATION}-iphoneos/${PROJECT_NAME}.framework" "${UNIVERSAL_OUTPUTFOLDER}/iOS"

# copy existing Swift modules from iphonesimulator build to the universal framework directory
SIMULATOR_SWIFT_MODULES_DIR="${BUILD_DIR}/${CONFIGURATION}-iphonesimulator/${PROJECT_NAME}.framework/Modules/${PROJECT_NAME}.swiftmodule/"
if [ -d "${SIMULATOR_SWIFT_MODULES_DIR}" ]; then
    cp -R "${SIMULATOR_SWIFT_MODULES_DIR}" "${UNIVERSAL_OUTPUTFOLDER}/iOS/${PROJECT_NAME}.framework/Modules/${PROJECT_NAME}.swiftmodule"
fi

# create universal binary file using lipo and place the combined executable in the universal framework directory
lipo -create -output "${UNIVERSAL_OUTPUTFOLDER}/iOS/${PROJECT_NAME}.framework/${PROJECT_NAME}" "${BUILD_DIR}/${CONFIGURATION}-iphonesimulator/${PROJECT_NAME}.framework/${PROJECT_NAME}" "${BUILD_DIR}/${CONFIGURATION}-iphoneos/${PROJECT_NAME}.framework/${PROJECT_NAME}"

# intermediate step for copying the framework to the project's directory
mkdir -p "${TMPDIR}/${PROJECT_NAME}/Frameworks/iOS"
cp -R "${UNIVERSAL_OUTPUTFOLDER}/iOS/${PROJECT_NAME}.framework" "${TMPDIR}/${PROJECT_NAME}/Frameworks/iOS"

# create a zip file and move it to the project's directory
cd "${TMPDIR}/${PROJECT_NAME}/Frameworks/iOS"
zip -r "${PROJECT_NAME}.framework.zip" "${PROJECT_NAME}.framework"
mkdir -p "${PROJECT_DIR}/universal-framework"
mv "${PROJECT_NAME}.framework.zip" "${PROJECT_DIR}/universal-framework"

# optional: show the project's directory in Finder
open "${PROJECT_DIR}/universal-framework"
fi
于 2017-09-29T12:31:28.693 回答