问题描述
我正在开发一个依赖Alamofire的小型Swift框架.我将其用作属于同一工作空间的应用程序的嵌入式框架,并且运行良好.
I'm developing a small Swift framework that depends on Alamofire. I'm using it as an embedded framework of an app belonging to the same workspace and it works perfectly.
当我想构建一个带有聚合目标的通用框架时,就会出现问题.然后,在执行脚本生成框架时,它失败,并显示消息No such module 'Alamofire'
,引用了我的一个源文件中的import Alamofire
.
The problem arises when I want to build an universal framework with an aggregate target. Then, when executing the script to generate the framework it fails with the message No such module 'Alamofire'
, referring to an import Alamofire
in one of my source files.
这是我的Podfile:
This is my Podfile:
platform :ios, '9.0'
use_frameworks!
inhibit_all_warnings!
target 'FSIBackend' do
pod 'SwiftLint'
pod 'Alamofire'
pod 'SwiftyJSON'
end
这是生成框架的脚本.它可以与其他没有Pods依赖项的框架一起使用,所以我认为这是可以的:
This is the script to generate the framework. It works with other frameworks without Pods dependencies so I assume that is ok:
set -e
# Setup
FRAMEWORK_NAME="${1}"
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创建通用框架,因此我不知道自己是否在做一些不可能的事情.
The question is that I don't know how to build my framework depending on Alamofire. Do I have to create a podspec for my framework and use it via CocoaPods? This is the first time I create a universal framework depending on a pod so I don't know if I'm doing something impossible.
非常感谢您.
推荐答案
最后我可以考虑到@mag_zbc的建议来完成它,谢谢.
Finally I could accomplish it taking into account the advice given from @mag_zbc, thank you.
我必须以这种方式修改框架生成:
I had to modify the framework generation this way:
set -e
# Setup
WORKSPACE="${1}"
FRAMEWORK_NAME="${2}"
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.
After generated, and added to the consumer app, the only thing left to do is to use Cocoapods in the consumer app to get Alamofire and SwiftyJSON.
这篇关于基于Pod的Swift通用框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!