在 Swift 框架中导入 CommonCrypto [英] Importing CommonCrypto in a Swift framework

查看:48
本文介绍了在 Swift 框架中导入 CommonCrypto的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How do you import CommonCrypto in a Swift framework for iOS?

我了解如何在 Swift 应用程序中使用 CommonCrypto:您将 #import 添加到桥接头.但是,Swift 框架不支持桥接头.文档 说:

I understand how to use CommonCrypto in a Swift app: You add #import <CommonCrypto/CommonCrypto.h> to the bridging header. However, Swift frameworks don't support bridging headers. The documentation says:

您可以导入具有纯 Objective-C 代码库、纯 Swift 代码库或混合语言代码库的外部框架.这导入外部框架的过程是一样的框架是用一种语言编写的,或者包含来自这两种语言的文件语言.导入外部框架时,请确保为您正在导入的框架定义模块构建设置是.

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.

您可以将框架导入到不同的 Swift 文件中目标使用以下语法:

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

import FrameworkName

不幸的是,导入 CommonCrypto 不起作用.也不会将 #import 添加到伞头.

Unfortunately, import CommonCrypto doesn't work. Neither does adding #import <CommonCrypto/CommonCrypto.h> to the umbrella header.

推荐答案

我发现了一个在 Swift 框架中成功使用 CommonCrypto 的 GitHub 项目:SHA256-Swift.此外,这篇关于 与 sqlite3 相同的问题 的文章是有用.

I found a GitHub project that successfully uses CommonCrypto in a Swift framework: SHA256-Swift. Also, this article about the same problem with sqlite3 was useful.

基于以上,步骤为:

1) 在项目目录中创建一个 CommonCrypto 目录.在其中创建一个 module.map 文件.模块映射将允许我们将 CommonCrypto 库用作 Swift 中的模块.其内容为:

1) Create a CommonCrypto directory inside the project directory. Within, create a module.map file. The module map will allow us to use the CommonCrypto library as a module within Swift. Its contents are:

module CommonCrypto [system] {
    header "/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator8.0.sdk/usr/include/CommonCrypto/CommonCrypto.h"
    link "CommonCrypto"
    export *
}

2) 在 Build Settings 中,在 Swift Compiler - Search Paths 中,将 CommonCrypto 目录添加到 Import Paths (SWIFT_INCLUDE_PATHS).

2) In Build Settings, within Swift Compiler - Search Paths, add the CommonCrypto directory to Import Paths (SWIFT_INCLUDE_PATHS).

3) 最后,在 Swift 文件中像任何其他模块一样导入 CommonCrypto.例如:

3) Finally, import CommonCrypto inside your Swift files as any other modules. For example:

import CommonCrypto

extension String {

    func hnk_MD5String() -> String {
        if let data = self.dataUsingEncoding(NSUTF8StringEncoding)
        {
            let result = NSMutableData(length: Int(CC_MD5_DIGEST_LENGTH))
            let resultBytes = UnsafeMutablePointer<CUnsignedChar>(result.mutableBytes)
            CC_MD5(data.bytes, CC_LONG(data.length), resultBytes)
            let resultEnumerator = UnsafeBufferPointer<CUnsignedChar>(start: resultBytes, length: result.length)
            let MD5 = NSMutableString()
            for c in resultEnumerator {
                MD5.appendFormat("%02x", c)
            }
            return MD5
        }
        return ""
    }
}

限制

在另一个项目中使用自定义框架在编译时失败,并显示错误缺少必需的模块CommonCrypto".这是因为 CommonCrypto 模块似乎没有包含在自定义框架中.解决方法是在使用框架的项目中重复步骤 2(设置 Import Paths).

Limitations

Using the custom framework in another project fails at compile time with the error missing required module 'CommonCrypto'. This is because the CommonCrypto module does not appear to be included with the custom framework. A workaround is to repeat step 2 (setting Import Paths) in the project that uses the framework.

模块映射不是平台无关的(它目前指向一个特定的平台,iOS 8 模拟器).我不知道如何制作相对于当前平台的标题路径.

The module map is not platform independent (it currently points to a specific platform, the iOS 8 Simulator). I don't know how to make the header path relative to the current platform.

iOS 8 的更新 <= 我们应该删除 link "CommonCrypto" 行,以获得成功的编译.

Updates for iOS 8 <= We should remove the line link "CommonCrypto", to get the successful compilation.

更新/编辑

我不断收到以下构建错误:

I kept getting the following build error:

ld:找不到用于架构 x86_64 的 -lCommonCrypto 的库clang:错误:链接器命令失败,退出代码为 1(使用 -v 查看调用)

ld: library not found for -lCommonCrypto for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)

除非我从我创建的 module.map 文件中删除了 link "CommonCrypto" 行.一旦我删除了这条线,它就可以正常构建.

Unless I removed the line link "CommonCrypto" from the module.map file I created. Once I removed this line it built ok.

这篇关于在 Swift 框架中导入 CommonCrypto的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆