Importing CommonCrypto in a Swift framework
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) 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) In Build Settings, within Swift Compiler - Search Paths, add the CommonCrypto
directory to Import Paths (SWIFT_INCLUDE_PATHS
).
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 ""
}
}
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.
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.
Updates for iOS 8 <= We should remove the line link "CommonCrypto", to get the successful compilation.
UPDATE / EDIT
I kept getting the following build error:
ld: library not found for -lCommonCrypto for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Unless I removed the line link "CommonCrypto"
from the module.map
file I created. Once I removed this line it built ok.
Can't import CommonCrypto in mixed language framework
I've encountered this very problem myself. Here's how you resolve it:
- Create a module map file (here's my file).
- Copy the latest
CommonCrypto.h
header. - Create a directory
CommonCrypto
for both these files. - Copy the directory (via drag-and-drop) to your project.
- Add the directory path under SWIFT_INCLUDE_PATHS for your target framework.
This should allow you to use import CommonCrypto
wherever you want (for Swift, not Objective-C).
Edit: Seems like I misread the question initially. You want to use CommonCrypto in Objective-C and then use that from Swift. Here's some advice: don't #import
CommonCrypto in your public headers, but rather just internally. Wrap all your crypto-structures so that there's no public dependency for CommonCrypto whatsoever, and then just use it from Swift via the default bridging procedure.
Swift 3 import CommonCrypto
I know there are similar questions on stackoverflow about this but I looked at them and still had problems so I wanted to share my experiences.
Easiest way to import the Obj-C CommonCrypto library to an existing Swift XCode Project (Swift 3, Xcode 8.3.3):
- Add a new file of type "Objective-C file". It doesn't matter what you call it, you will delete it in a moment.
- After you add that file Xcode should prompt you if you want to create a bridging header. Check the appropriate targets for you project and allow Xcode to create the bridging header for you.
- Add #import < CommonCrypto/CommonCrypto.h > to the bridging header(s) it creates.
- Delete the Objective-C file you created in step 1.
I tried to create my own Objective-C bridging file and it wasn't working. I spent about an hour looking for solutions until I tried this. I wanted to share to hopefully spare other developers the issue I had.
CommonCrypto for Framework in podspec
Nevermind, It seems like I'm not the only failing to include CommonCrypto in a SDK included in another SDK.
I just bypass the problem by including CryptoSwift (using only pure Swift). It works perfectly for me. It's a bit heavy but you don't have to deal with modulemap files and C library... Pretty easy to work with, nice implementation
Here the link of CryptoSwift : https://github.com/krzyzanowskim/CryptoSwift
Hope it will help one of you !
PS : can anyone explain me why did I get down voted ? :(
Related Topics
How to Use Scnetworkreachability in Swift
Change User Agent in Uiwebview
Iphone 6 Plus Resolution Confusion: Xcode or Apple'S Website? For Development
Setting Action For Back Button in Navigation Controller
Objective-C and Swift Url Encoding
How to Add Uitableview Within a Uitableviewcell
Starting Iphone App Development in Linux
How to Apply Gradient to Background View of iOS Swift App
How to Get Current Location from User in Ios
Uploading File With Parameters Using Alamofire
Returning Method Object from Inside Block
Move View With Keyboard Using Swift
Is [Uiscreen Mainscreen].Bounds.Size Becoming Orientation-Dependent in Ios8
How to Access a Specific Field from Cloud Firestore Firebase in Swift
Update Badge With Push Notification While App in Background
How to Share Data Between View Controllers and Other Objects in Swift