问题描述
如何在适用于iOS的Swift框架中导入CommonCrypto?
How do you import CommonCrypto in a Swift framework for iOS?
我了解如何在Swift应用程序中使用CommonCrypto:添加#导入< CommonCrypto / CommonCrypto.h>
到桥接头。
I understand how to use CommonCrypto in a Swift app: you add #import <CommonCrypto/CommonCrypto.h>
to the bridging header.
但是,Swift框架不支持桥接头。 说:
However, Swift frameworks don't support bridging headers. The documentation says:
您可以使用以下语法将框架导入到不同的
目标中的任何Swift文件中:
You can import a framework into any Swift file within a different target using the following syntax:
import FrameworkName
不幸的是,导入 CommonCrypto
不起作用。也没有将 #import< CommonCrypto / CommonCrypto.h>
添加到伞形标题中。
Unfortunately, import CommonCrypto
doesn't work. Neither does adding #import <CommonCrypto/CommonCrypto.h>
to the umbrella header.
推荐答案
我发现了一个在Swift框架中成功使用CommonCrypto的GitHub项目:。此外,这篇文章关于很有用。
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.
根据以上所述,步骤如下:
Based on the above, the steps are:
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)在构建设置中,在 Swift编译器 - 搜索路径中,添加 CommonCrypto
目录到导入路径( 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(设置导入路径
)。
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.
模块map不是平台独立的(它目前指向一个特定的平台,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的更新< =我们应该删除行链接CommonCrypto ,以获得成功的编辑。
Updates for iOS 8 <= We should remove the line link "CommonCrypto", to get the successful compilation.
更新/编辑
我一直收到以下构建错误:
I kept getting the following build error:
除非我删除了行从我创建的
。一旦我删除了这一行,它就构建好了。 module.map
文件中链接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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!