Swift 依赖 pod 的通用框架
Swift universal framework depending on pod
我正在开发一个依赖于 Alamofire 的小型 Swift 框架。我将它用作属于同一工作区的应用程序的嵌入式框架,并且运行良好。
当我想构建一个带有聚合目标的通用框架时,问题就出现了。然后,当执行生成框架的脚本时,它失败并显示消息 No such module 'Alamofire'
,指的是我的一个源文件中的 import Alamofire
。
这是我的 Podfile:
platform :ios, '9.0'
use_frameworks!
inhibit_all_warnings!
target 'FSIBackend' do
pod 'SwiftLint'
pod 'Alamofire'
pod 'SwiftyJSON'
end
这是生成框架的脚本。它适用于没有 Pods 依赖项的其他框架,所以我认为没问题:
set -e
# Setup
FRAMEWORK_NAME=""
BUILD_DIR="${SRCROOT}/build"
OUTPUT_DIR="${HOME}/Desktop/"
OUTPUT="${OUTPUT_DIR}/${FRAMEWORK_NAME}.framework"
rm -rf "${BUILD_DIR}"
rm -rf "${OUTPUT}"
mkdir -p "${OUTPUT_DIR}"
# Build
xcodebuild -target "${FRAMEWORK_NAME}" -configuration Release -arch arm64 -arch armv7 -arch armv7s only_active_arch=no defines_module=yes -sdk "iphoneos"
xcodebuild -target "${FRAMEWORK_NAME}" -configuration Release -arch x86_64 -arch i386 only_active_arch=no defines_module=yes -sdk "iphonesimulator"
# Copy the device version of framework to output.
cp -r "${BUILD_DIR}/Release-iphoneos/${FRAMEWORK_NAME}.framework" "${OUTPUT}"
# Replace the framework executable within the framework with a new version created by merging the device and simulator frameworks' executables with lipo.
lipo -create -output "${OUTPUT}/${FRAMEWORK_NAME}" "${BUILD_DIR}/Release-iphoneos/${FRAMEWORK_NAME}.framework/${FRAMEWORK_NAME}" "${BUILD_DIR}/Release-iphonesimulator/${FRAMEWORK_NAME}.framework/${FRAMEWORK_NAME}"
# Copy the Swift module mappings for the simulator into the framework. The device mappings already exist from step 6.
cp -r "${BUILD_DIR}/Release-iphonesimulator/${FRAMEWORK_NAME}.framework/Modules/${FRAMEWORK_NAME}.swiftmodule/" "${OUTPUT}/Modules/${FRAMEWORK_NAME}.swiftmodule"
# Delete build.
rm -rf "${BUILD_DIR}"
问题是我不知道如何根据 Alamofire 构建我的框架。我是否必须为我的框架创建一个 podspec 并通过 CocoaPods 使用它?这是我第一次创建依赖于 pod 的通用框架,所以我不知道我是否在做一些不可能的事情。
非常感谢。
考虑到@mag_zbc的建议,终于完成了,谢谢。
我不得不这样修改框架生成:
set -e
# Setup
WORKSPACE=""
FRAMEWORK_NAME=""
BUILD_DIR="${SRCROOT}/build"
OUTPUT_DIR="${HOME}/Desktop/"
OUTPUT="${OUTPUT_DIR}/${FRAMEWORK_NAME}.framework"
CONFIGURATION="${CONFIGURATION}"
rm -rf "${BUILD_DIR}"
rm -rf "${OUTPUT}"
mkdir -p "${OUTPUT_DIR}"
# Build the framework for device and for simulator (using all needed architectures).
xcodebuild -workspace "${WORKSPACE}" -scheme "${FRAMEWORK_NAME}" -configuration ${CONFIGURATION} -arch x86_64 -arch i386 only_active_arch=no defines_module=yes -sdk "iphonesimulator" clean build CONFIGURATION_BUILD_DIR=${BUILD_DIR}/${CONFIGURATION}-iphonesimulator
xcodebuild -workspace "${WORKSPACE}" -scheme "${FRAMEWORK_NAME}" -configuration ${CONFIGURATION} -arch arm64 -arch armv7 -arch armv7s only_active_arch=no defines_module=yes -sdk "iphoneos" clean build CONFIGURATION_BUILD_DIR=${BUILD_DIR}/${CONFIGURATION}-iphoneos
# Copy the device version of framework to output.
cp -r "${BUILD_DIR}/${CONFIGURATION}-iphoneos/${FRAMEWORK_NAME}.framework" "${OUTPUT}"
# Replace the framework executable within the framework with a new version created by merging the device and simulator frameworks' executables with lipo.
lipo -create -output "${OUTPUT}/${FRAMEWORK_NAME}" "${BUILD_DIR}/${CONFIGURATION}-iphoneos/${FRAMEWORK_NAME}.framework/${FRAMEWORK_NAME}" "${BUILD_DIR}/${CONFIGURATION}-iphonesimulator/${FRAMEWORK_NAME}.framework/${FRAMEWORK_NAME}"
# Copy the Swift module mappings for the simulator into the framework. The device mappings already exist from step 6.
cp -r "${BUILD_DIR}/${CONFIGURATION}-iphonesimulator/${FRAMEWORK_NAME}.framework/Modules/${FRAMEWORK_NAME}.swiftmodule/" "${OUTPUT}/Modules/${FRAMEWORK_NAME}.swiftmodule"
# Delete build.
rm -rf "${BUILD_DIR}"
生成并添加到消费者应用程序后,唯一剩下要做的就是在消费者应用程序中使用 Cocoapods 获取 Alamofire 和 SwiftyJSON。
我正在开发一个依赖于 Alamofire 的小型 Swift 框架。我将它用作属于同一工作区的应用程序的嵌入式框架,并且运行良好。
当我想构建一个带有聚合目标的通用框架时,问题就出现了。然后,当执行生成框架的脚本时,它失败并显示消息 No such module 'Alamofire'
,指的是我的一个源文件中的 import Alamofire
。
这是我的 Podfile:
platform :ios, '9.0'
use_frameworks!
inhibit_all_warnings!
target 'FSIBackend' do
pod 'SwiftLint'
pod 'Alamofire'
pod 'SwiftyJSON'
end
这是生成框架的脚本。它适用于没有 Pods 依赖项的其他框架,所以我认为没问题:
set -e
# Setup
FRAMEWORK_NAME=""
BUILD_DIR="${SRCROOT}/build"
OUTPUT_DIR="${HOME}/Desktop/"
OUTPUT="${OUTPUT_DIR}/${FRAMEWORK_NAME}.framework"
rm -rf "${BUILD_DIR}"
rm -rf "${OUTPUT}"
mkdir -p "${OUTPUT_DIR}"
# Build
xcodebuild -target "${FRAMEWORK_NAME}" -configuration Release -arch arm64 -arch armv7 -arch armv7s only_active_arch=no defines_module=yes -sdk "iphoneos"
xcodebuild -target "${FRAMEWORK_NAME}" -configuration Release -arch x86_64 -arch i386 only_active_arch=no defines_module=yes -sdk "iphonesimulator"
# Copy the device version of framework to output.
cp -r "${BUILD_DIR}/Release-iphoneos/${FRAMEWORK_NAME}.framework" "${OUTPUT}"
# Replace the framework executable within the framework with a new version created by merging the device and simulator frameworks' executables with lipo.
lipo -create -output "${OUTPUT}/${FRAMEWORK_NAME}" "${BUILD_DIR}/Release-iphoneos/${FRAMEWORK_NAME}.framework/${FRAMEWORK_NAME}" "${BUILD_DIR}/Release-iphonesimulator/${FRAMEWORK_NAME}.framework/${FRAMEWORK_NAME}"
# Copy the Swift module mappings for the simulator into the framework. The device mappings already exist from step 6.
cp -r "${BUILD_DIR}/Release-iphonesimulator/${FRAMEWORK_NAME}.framework/Modules/${FRAMEWORK_NAME}.swiftmodule/" "${OUTPUT}/Modules/${FRAMEWORK_NAME}.swiftmodule"
# Delete build.
rm -rf "${BUILD_DIR}"
问题是我不知道如何根据 Alamofire 构建我的框架。我是否必须为我的框架创建一个 podspec 并通过 CocoaPods 使用它?这是我第一次创建依赖于 pod 的通用框架,所以我不知道我是否在做一些不可能的事情。
非常感谢。
考虑到@mag_zbc的建议,终于完成了,谢谢。
我不得不这样修改框架生成:
set -e
# Setup
WORKSPACE=""
FRAMEWORK_NAME=""
BUILD_DIR="${SRCROOT}/build"
OUTPUT_DIR="${HOME}/Desktop/"
OUTPUT="${OUTPUT_DIR}/${FRAMEWORK_NAME}.framework"
CONFIGURATION="${CONFIGURATION}"
rm -rf "${BUILD_DIR}"
rm -rf "${OUTPUT}"
mkdir -p "${OUTPUT_DIR}"
# Build the framework for device and for simulator (using all needed architectures).
xcodebuild -workspace "${WORKSPACE}" -scheme "${FRAMEWORK_NAME}" -configuration ${CONFIGURATION} -arch x86_64 -arch i386 only_active_arch=no defines_module=yes -sdk "iphonesimulator" clean build CONFIGURATION_BUILD_DIR=${BUILD_DIR}/${CONFIGURATION}-iphonesimulator
xcodebuild -workspace "${WORKSPACE}" -scheme "${FRAMEWORK_NAME}" -configuration ${CONFIGURATION} -arch arm64 -arch armv7 -arch armv7s only_active_arch=no defines_module=yes -sdk "iphoneos" clean build CONFIGURATION_BUILD_DIR=${BUILD_DIR}/${CONFIGURATION}-iphoneos
# Copy the device version of framework to output.
cp -r "${BUILD_DIR}/${CONFIGURATION}-iphoneos/${FRAMEWORK_NAME}.framework" "${OUTPUT}"
# Replace the framework executable within the framework with a new version created by merging the device and simulator frameworks' executables with lipo.
lipo -create -output "${OUTPUT}/${FRAMEWORK_NAME}" "${BUILD_DIR}/${CONFIGURATION}-iphoneos/${FRAMEWORK_NAME}.framework/${FRAMEWORK_NAME}" "${BUILD_DIR}/${CONFIGURATION}-iphonesimulator/${FRAMEWORK_NAME}.framework/${FRAMEWORK_NAME}"
# Copy the Swift module mappings for the simulator into the framework. The device mappings already exist from step 6.
cp -r "${BUILD_DIR}/${CONFIGURATION}-iphonesimulator/${FRAMEWORK_NAME}.framework/Modules/${FRAMEWORK_NAME}.swiftmodule/" "${OUTPUT}/Modules/${FRAMEWORK_NAME}.swiftmodule"
# Delete build.
rm -rf "${BUILD_DIR}"
生成并添加到消费者应用程序后,唯一剩下要做的就是在消费者应用程序中使用 Cocoapods 获取 Alamofire 和 SwiftyJSON。