gpt4 book ai didi

ios - 在 Swift 框架中导入 CommonCrypto

转载 作者:IT王子 更新时间:2023-10-29 04:55:06 27 4
gpt4 key购买 nike

如何导入 CommonCrypto在 iOS 的 Swift 框架中?

我了解如何使用 CommonCrypto在 Swift 应用程序中:您添加 #import <CommonCrypto/CommonCrypto.h>到桥接头。但是,Swift 框架不支持桥接 header 。 documentation说:

You can import external frameworks that have a pure Objective-C codebase, a pure Swift codebase, or a mixed-language codebase. The process for importing an external framework is the same whether the framework is written in a single language or contains files from both languages. When you import an external framework, make sure the Defines Module build setting for the framework you’re importing is set to Yes.

You can import a framework into any Swift file within a different target using the following syntax:

import FrameworkName

不幸的是,导入 CommonCrypto不起作用。添加 #import <CommonCrypto/CommonCrypto.h> 也不行到伞头。

最佳答案

更简单、更健壮的方法是创建一个名为“CommonCryptoModuleMap”的聚合目标,并在运行脚本阶段自动生成模块映射并使用正确的 Xcode/SDK 路径:

enter image description here enter image description here

运行脚本阶段应该包含这个 bash:

# This if-statement means we'll only run the main script if the CommonCryptoModuleMap directory doesn't exist
# Because otherwise the rest of the script causes a full recompile for anything where CommonCrypto is a dependency
# Do a "Clean Build Folder" to remove this directory and trigger the rest of the script to run
if [ -d "${BUILT_PRODUCTS_DIR}/CommonCryptoModuleMap" ]; then
echo "${BUILT_PRODUCTS_DIR}/CommonCryptoModuleMap directory already exists, so skipping the rest of the script."
exit 0
fi

mkdir -p "${BUILT_PRODUCTS_DIR}/CommonCryptoModuleMap"
cat <<EOF > "${BUILT_PRODUCTS_DIR}/CommonCryptoModuleMap/module.modulemap"
module CommonCrypto [system] {
header "${SDKROOT}/usr/include/CommonCrypto/CommonCrypto.h"
export *
}
EOF

使用 shell 代码和 ${SDKROOT} 意味着您不必对 Xcode.app 路径进行硬编码,这可能会因系统而异,尤其是如果您使用 xcode-选择 切换到 beta 版本,或者在 CI 服务器上构建,其中多个版本安装在非标准位置。您也不需要对 SDK 进行硬编码,因此它应该适用于 iOS、macOS 等。您也不需要在项目的源目录中放置任何东西。

创建这个目标后,让你的库/框架依赖于它的目标依赖项:

enter image description here

这将确保在构建框架之前生成模块映射。

ma​​cOS 注释:如果您也支持 macOS,则需要将 macosx 添加到 Supported Platforms 在您刚刚创建的新聚合目标上build设置,否则它不会将模块映射与其余框架产品一起放入正确的 Debug 派生数据文件夹中。

enter image description here

接下来,将模块映射的父目录 ${BUILT_PRODUCTS_DIR}/CommonCryptoModuleMap 添加到 Swift 部分下的“导入路径”build设置 (SWIFT_INCLUDE_PATHS):

enter image description here

如果您在项目或 xcconfig 级别定义了搜索路径,请记住添加 $(inherited) 行。

就是这样,您现在应该能够导入 CommonCrypto

Xcode 10 更新

Xcode 10 现在附带了一个 CommonCrypto 模块映射,因此不需要这种解决方法。如果您想同时支持 Xcode 9 和 10,您可以在运行脚本阶段检查模块映射是否存在,例如

COMMON_CRYPTO_DIR="${SDKROOT}/usr/include/CommonCrypto"
if [ -f "${COMMON_CRYPTO_DIR}/module.modulemap" ]
then
echo "CommonCrypto already exists, skipping"
else
# generate the module map, using the original code above
fi

关于ios - 在 Swift 框架中导入 CommonCrypto,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25248598/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com