Importing CommonCrypto in a Swift framework
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Importing CommonCrypto in a Swift app target is easier than it used to be, but framework targets have historically been the place where people get stuck. The big reason is that framework targets cannot rely on the usual bridging-header trick the way app targets often do.
So the right answer depends on your toolchain. In modern Xcode and SDKs, try a direct import CommonCrypto first. If that is not available in your environment, the fallback is to expose CommonCrypto as a Clang module rather than trying to force a bridging header into the framework target.
Try the Direct Import First
On current Apple toolchains, this often works directly in Swift:
If that builds, you are done. This is the cleanest outcome.
Why the Old Bridging-Header Advice Breaks Down
A lot of older posts suggest creating an Objective-C bridging header and importing CommonCrypto there. That advice works for many app targets, but framework targets are the problem case.
If you are building a reusable Swift framework, relying on a bridging header is usually the wrong direction. The framework should see CommonCrypto as a proper module import instead.
That is why modern guidance is either:
- direct
import CommonCrypto - or a module-map based fallback for older toolchains
The Module-Map Fallback
If import CommonCrypto is not available directly, expose it as a Clang module. The concept is:
- create a
module.modulemap - point Swift's include search path at the directory containing it
- import
CommonCryptonormally from Swift
A minimal module map looks like this:
The exact file layout and include path depend on how your project is organized, but the important idea is that Swift imports a module, not a bridging header, inside the framework target.
A Practical Framework Setup
A common project structure is:
Then add the Modules directory to the framework target's import or include search paths so the Swift compiler can resolve import CommonCrypto.
Once the module is visible, your Swift code can stay clean:
That is much better than scattering C-import workarounds throughout the framework.
Example: MD5 Hashing in a Framework Utility
Here is a second example using CommonCrypto from Swift code that could live inside a framework utility type:
The CommonCrypto API is C-based, so the Swift code mainly deals with pointer-safe access to the input bytes and digest buffer.
When to Consider CryptoKit Instead
If your minimum platform versions allow it and you do not specifically need CommonCrypto, CryptoKit is often the more modern Swift-native choice.
But CommonCrypto still appears often in shared codebases, older deployment targets, and framework work where consistency with existing C-based crypto code matters.
Common Pitfalls
The biggest pitfall is using app-target advice for a framework target. A bridging header is often the wrong tool in that situation.
Another common issue is assuming a custom module map is always necessary. On newer Apple toolchains, try import CommonCrypto first because the direct import may already work.
Developers also forget that CommonCrypto is a C API. Importing it successfully is only half the job; the Swift wrapper code still needs careful pointer handling.
Finally, think twice before adding crypto code just to hash strings casually. If the project targets modern Apple platforms and does not require CommonCrypto specifically, CryptoKit may be simpler.
Summary
- In a Swift framework, try
import CommonCryptodirectly first. - Framework targets should not depend on the usual bridging-header advice meant for app targets.
- If direct import is unavailable, expose CommonCrypto through a module map instead.
- Once the module is visible, Swift code can call C hashing APIs such as
CC_SHA256orCC_MD5normally. - Consider
CryptoKitfor newer platform targets when CommonCrypto is not a hard requirement.
Related reading
- Importing Project-Swift.h into a Objective-C class...file not found
- Importing Project-Swift.h into a Objective-C class...file not found
- Impossible to hide navigation bars in Safari iOS 7 for iPhone/iPod touch
- In-App Purchases stuck in Missing Metadata state
- In a storyboard, how do I make a custom cell for use with multiple controllers?
- In absence of preprocessor macros, is there a way to define practical scheme specific flags at project level in Xcode project
- In absence of preprocessor macros, is there a way to define practical scheme specific flags at project level in Xcode project
- In android app Toolbar.setTitle method has no effect – application name is shown as title
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.