Swift通用框架取决于pod

时间:2017-07-19 13:43:41

标签: ios swift cocoapods ios-universal-framework

我正在开发一个依赖于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="${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创建一个通用框架,所以我不知道我是否做了不可能的事情。

非常感谢你。

1 个答案:

答案 0 :(得分:4)

最后,考虑到@mag_zbc给出的建议,我可以完成它,谢谢。

我必须以这种方式修改框架:

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。