Importing CommonCrypto in a Swift framework
Asked Answered
M

16

190

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

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:

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

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

Mandolin answered 11/8, 2014 at 17:3 Comment(7)
CommonCrypto is a C-based framework, not an Objective-C framework.Contango
@Contango Objective-C is a C superset. Are you saying we can't use CommonCrypto from Swift?Mandolin
@Contango I just managed to get CommonCrypto working by using module maps. I will polish the solution and post it later today.Mandolin
if you find it convenience, and what you looking for is already implemented, you can give a try to CryptoSwiftOutmaneuver
Apple just open sourced CommonCrypto. Maybe we can get it running if we have the sources.Antifreeze
Is there a timeframe for a real solution, rather than one of these hacks. This and ABI compatibility make me think Apple doesn't get it or just doesn't care. I'm seriously thinking of porting my frameworks back to Objective C. I don't like the language, but at least I don't have to relearn the String class every release.Chapple
In my framework i have created Objective-C class named MyProjectCrypto, and in that class included CommonCrypto. I use this class in my Umbrella Header, and in swift source files. So this is just Objective-C Facade around C framework.Oligopsony
M
84

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).

Build Settings

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.

Mandolin answered 11/8, 2014 at 19:2 Comment(17)
Gee, Apple sure want's to make things difficult. Would it kill the Swifties to just let us import files/frameworks without having to go through all this BS?Jacie
This is infuriating since the $SDKROOT variable is supposed to allow you platform agnostic paths, but I have no idea how to get to that in Swift.Melvamelvena
I got a compile error, var result:CCCryptorStatus = CCCrypt( kCCEncrypt, kCCAlgorithm3DES, kCCOptionPKCS7Padding | kCCOptionECBMode, key, kCCKeySize3DES ..,Chub
I got a compile error, it occurred the first line, var result:CCCryptorStatus = CCCrypt(kCCEncrypt... it tells 'Int' is not convertible to CCOperation. do you know how to solve it?Chub
I've been trying this with NMSSH, but I keep getting ld: library not found for -lNMSSH for architecture arm64 as well as for armv7. I can't figure out why.Ear
@Zaph, your statement could be rephrased as: Gee C makes it really hard to avoid security holes and bugs.Broadfaced
@porcoesphino Ah, Swift would have made Heartbleed impossible? Eliminate poor crypto practices? UnsafeBufferPointer is unnecessary? It is not the language, it is the programmers. Even the goto fail bug was mainly due to violating the DRY principle. Heaven help the person saying that "The Emperor has no clothes."Jacie
Kkk. You know my comment wasn't saying Swift was perfect. I've never coded in Swift but have coded a lot in C. Still it's easy to see that C allows developers to easily make mistakes that can be abused. Swift closes some of those holes. Apple likes closing holes (and pushing their agenda). It's annoying but hardly a surprise. What do they have to gain from doing what you're complaining about? They do have things to lose.Broadfaced
I'm wondering if Swift 1.2 allows an easier mechanism for doing this?Umber
For me it didn't work until I removed the link "CommonCrypto" from the module.map file.Baillie
Still necessary in Xcode 7 beta 5. However, note that it appears to be no longer necessary to include the setting in the project using the framework.Umber
@Mandolin This solution appears to no longer work in Xcode 7.3 beta 1. Struggling to find another solution. See: #34772739Umber
Can anyone confirm this working on Xcode 7.3? This solution stopped working for me after the update.Mould
Correnction: it's working fine when I build for simulator, but fails @ linking when I build for an iOS 9.3 device with "ld: library not found for -lCommonCrypto for architecture arm64"Mould
@OdedRegev I just followed the instructions in this answer and everything works for me now.Mould
It's working for me but out of curiosity... you only have a link to the simulator sdk but it's working on my device?!? how? :)Monoicous
There is so much BS with Apple XCode and Swift, I dont know where to start. Are you serious ? A absolute path in project setttings file, how in earth am I going to get a team to work on this ? They have to chnage the path everytime before they start work ? Really ? /Applications/Xcode6-Beta5.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator8.0.sdk/usr/include/CommonCrypto/CommonCrypto.h"Bolognese
H
141

Something a little simpler and more robust is to create an Aggregate target called "CommonCryptoModuleMap" with a Run Script phase to generate the module map automatically and with the correct Xcode/SDK path:

enter image description here enter image description here

The Run Script phase should contain this 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

Using shell code and ${SDKROOT} means you don't have to hard code the Xcode.app path which can vary system-to-system, especially if you use xcode-select to switch to a beta version, or are building on a CI server where multiple versions are installed in non-standard locations. You also don't need to hard code the SDK so this should work for iOS, macOS, etc. You also don't need to have anything sitting in your project's source directory.

After creating this target, make your library/framework depend on it with a Target Dependencies item:

enter image description here

This will ensure the module map is generated before your framework is built.

macOS note: If you're supporting macOS as well, you'll need to add macosx to the Supported Platforms build setting on the new aggregate target you just created, otherwise it won't put the module map in the correct Debug derived data folder with the rest of the framework products.

enter image description here

Next, add the module map's parent directory, ${BUILT_PRODUCTS_DIR}/CommonCryptoModuleMap, to the "Import Paths" build setting under the Swift section (SWIFT_INCLUDE_PATHS):

enter image description here

Remember to add a $(inherited) line if you have search paths defined at the project or xcconfig level.

That's it, you should now be able to import CommonCrypto

Update for Xcode 10

Xcode 10 now ships with a CommonCrypto module map making this workaround unnecessary. If you would like to support both Xcode 9 and 10 you can do a check in the Run Script phase to see if the module map exists or not, e.g.

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
Hardened answered 17/3, 2017 at 8:38 Comment(18)
Late to the game on this one - but this should be the chosen answer. It's simple and is easier for other dev's working on the same project to see the requirement.Amoritta
If I do this in my .framework , should I have to do the same in the projects where I include this framework?Assai
This solves the problem of using a C library within a framework, but the path generated with the run script is not portable across machines, i.e. developer laptop, CI build machine etc. Including a framework using a module map as mentioned in this solution doesn't seem to work across different machines with different Xcode and SDK paths.Temikatemp
The run script's path is calculated using the correct build variables and I can verify it works in a large team across many different developers, multiple CI servers running versions of Xcode in various different locations. What issue/errors are you seeing?Hardened
@MikeWeller Hello, I did follow the steps described above, but getting No such module 'CommonCrypto'. I use xCode 9 now. Is there something more with the new version of xCode to make it work? Or I am missing something.Chico
Found that. Wrong reference for module.Chico
@Chico did you success with xCode9.Pascale
@KrishnarjunBanoth Actually I needed to move forward really fast, so I just copied the resulted module into the correct directory. Probably gonna look at that later to fix it. There is some wrong with BUILT_PRODUCTS_DIR which point to incorrect directory. Once I solve that I let know.Chico
This worked great, but unfortunately we saw (perhaps obviously, now I think about it) that linking against this Aggregate Build Target in framework targets in our project caused their source to be recompiled from scratch every time when building the main app target. Doh :(Privy
@Chico : I need to release my (broken) framework urgently and I'm facing the same issue, can you describe the step of "copied the resulted module into the correct directory." ?Eyeglasses
@IanDundas I updated the code above with a fix for the re-compilation issue as well as a fix for using this on macOSFollower
I am getting error - "No such module 'CommonCrypto'", I have created framework project and it uses Common crypto. I am installing my framework using cocoapod. I think in my case module map is not generating when use framework in other project using cocoa pod, Any solution?Angio
Surprised I made this mistake originally, but the run script phase might need to use ${TARGET_BUILD_DIR} since it is generating files for the current target, and in certain environments e.g. while archiving this can cause issues. Looks like Xcode 9.3 (beta) may have introduced changes which cause this to break. The header search path dependency should still use ${BUILT_PRODUCTS_DIR} however.Hardened
Looks like SWIFT_INCLUDE_PATHS also needs to have the module map's directory as of Xcode 9.3 beta to avoid "missing required module" errors from dependent frameworksHardened
Hey @MikeWeller. Thanks for the solution. Have you run into any issues with error messages for missing required architecture ? Looks like it is not being compiled for all of the architectures for some reason. Thanks!Woodworth
One possible improvement to your script build-step. Pay attention that you can define 'input files' and 'output files' to the build step, and then you do not need the conditional in the script, and Xcode will manage the need for rebuilding the module map automaticallySaxe
A little more love for @MikeWeller and his timely update on supporting Xcode 10!Jonasjonathan
This works great, but i'm not able to push this framework to pod repo for using through cocoapod. Do you have any solution for that?Hatch
L
95

You can actually build a solution that "just works" (no need to copy a module.modulemap and SWIFT_INCLUDE_PATHS settings over to your project, as required by other solutions here), but it does require you to create a dummy framework/module that you'll import into your framework proper. We can also ensure it works regardless of platform (iphoneos, iphonesimulator, or macosx).

  1. Add a new framework target to your project and name it after the system library, e.g., "CommonCrypto". (You can delete the umbrella header, CommonCrypto.h.)

  2. Add a new Configuration Settings File and name it, e.g., "CommonCrypto.xcconfig". (Don't check any of your targets for inclusion.) Populate it with the following:

    MODULEMAP_FILE[sdk=iphoneos*]        = \
        $(SRCROOT)/CommonCrypto/iphoneos.modulemap
    MODULEMAP_FILE[sdk=iphonesimulator*] = \
        $(SRCROOT)/CommonCrypto/iphonesimulator.modulemap
    MODULEMAP_FILE[sdk=macosx*]          = \
        $(SRCROOT)/CommonCrypto/macosx.modulemap
    
  3. Create the three referenced module map files, above, and populate them with the following:

    • iphoneos.modulemap

      module CommonCrypto [system] {
          header "/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk/usr/include/CommonCrypto/CommonCrypto.h"
          export *
      }
      
    • iphonesimulator.modulemap

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

      module CommonCrypto [system] {
          header "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/include/CommonCrypto/CommonCrypto.h"
          export *
      }
      

    (Replace "Xcode.app" with "Xcode-beta.app" if you're running a beta version. Replace 10.11 with your current OS SDK if not running El Capitan.)

  4. On the Info tab of your project settings, under Configurations, set the Debug and Release configurations of CommonCrypto to CommonCrypto (referencing CommonCrypto.xcconfig).

  5. On your framework target's Build Phases tab, add the CommonCrypto framework to Target Dependencies. Additionally add libcommonCrypto.dylib to the Link Binary With Libraries build phase.

  6. Select CommonCrypto.framework in Products and make sure its Target Membership for your wrapper is set to Optional.

You should now be able to build, run and import CommonCrypto in your wrapper framework.

For an example, see how SQLite.swift uses a dummy sqlite3.framework.

Lithometeor answered 22/3, 2015 at 1:8 Comment(25)
Works for me without step (5). With it I get a build error: ld: cannot link directly with /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator8.2.sdk/usr/lib/system/libcommonCrypto.dylib. Link against the umbrella framework 'System.framework' instead. for architecture x86_64Hostelry
@Hostelry I've updated the instructions to remove step 5 (it was unnecessary) and also account for a potential app store rejection around embedded sub-frameworks (see github.com/stephencelis/SQLite.swift/issues/88).Lithometeor
Excellent! Used this to make github.com/soffes/Crypto I didn't have to link System.framework though. It's worth noting, you have to make a separate wrapper framework for Mac and iOS if your framework is cross platform.Send
Amazing. Step 5 is not working for me either, but without - like a charm! I owe you a beer or two!Juieta
How or where do people find out stuff like this?Ripsaw
Just a note make it clear that you need to select Objective-C as language in step 1. This is easily overlooked. Also, perhaps because I didn't have a .dylib, I needed to add the .framework in step 5.Baillie
Getting "no such module CommonCrypto" always… Also doesn't have .dylib in step 5. XCode 7.2Impair
This solution works on our umbrella project but the tests this are failing with a "No such module: 'CommonCrypto'" error.Assembler
this is horrible. I have a zoo of Xcodes each broken in a different ways and having the absolute paths to the headers all over the place is puke invoking. Something is going awfully wrong in cupertino, or at least with whoever is in charge of this modulemap messChor
7.2.1 rolled out on feb 2nd and release notes says "The Swift compiler is now stricter about including non-modular header files. Debugging a Swift target requires that frameworks be properly modular, meaning all of the publicly-imported headers are either accounted for in the framework's umbrella header, imported from another modular framework, or listed explicitly in a custom module.modulemap file (advanced users only)." this is IMNSHO not for advanced users, this is a feature geared towards true masochistsChor
Note that if you are using CI this solution will easily break due to the hardcoded header paths. I'm still trying to figure out a workaround.Ellinger
Xcode 7.3 - Apple made some changes to CommonCrypto and now this solution doesn't work when running on real device. Anyone knows how to fix this? I get an error related to BitcodeShelba
Trying to build with this method causes build errors using Xcode 7.3Guberniya
Can't make it work at all, it won't build except when running a test on my machine.Blackheart
I get the following error when running on real device: "CommonCrypto does not contain bitcode. You must rebuild it with bitcode enabled (Xcode setting ENABLE_BITCODE), obtain an updated library from the vendor, or disable bitcode for this target. for architecture arm64"Shelba
Just to clarify, as of June 6, 2016, I am able to get this working on both the device and simulator with the latest Xcode release following steps 1–4. Steps 5 & 6 were not needed for me. I am producing a Universal (device and simulator) framework.Lovely
Update: I asked some Apple engineers yesterday and they said modulemap is the way to go.Antifreeze
Note that you must download the command line tools or none of the above will work. xcode-select --install will solve the problem.Chapple
Not sure if this is a good solution, but I was able to build and run on my device. #38800935Liguria
Is it possible to replace at least some of /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/ in the .modulemap with something semantic, such as $SDKROOT, or something? It seems a bit bonkers that there's not way to provide a relative path as of Xcode 8. Thanks!Nicolas
I am using Xcode 8.2.1, and I do not find "libcommonCrypto.dylib" mentioned in step 5. What is the alternative to that?Spoof
I can't hardcode a specific version of OSX in the path. Other people may checkout the code and have a different version of the OS. Is there an environment variable for that stuff?Dell
@Dell You should be able to use "MacOSX.sdk" instead of "MacOSX10.11.sdk" in the path.Luellaluelle
@HuseinBehboodiRad because you can't use a bridging header in a swift framework.Dell
I'm not sure if/when this changed, but for me, this path works. It should work in future releases, too: /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/CommonCrypto/CommonCrypto.hLuteous
M
84

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).

Build Settings

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.

Mandolin answered 11/8, 2014 at 19:2 Comment(17)
Gee, Apple sure want's to make things difficult. Would it kill the Swifties to just let us import files/frameworks without having to go through all this BS?Jacie
This is infuriating since the $SDKROOT variable is supposed to allow you platform agnostic paths, but I have no idea how to get to that in Swift.Melvamelvena
I got a compile error, var result:CCCryptorStatus = CCCrypt( kCCEncrypt, kCCAlgorithm3DES, kCCOptionPKCS7Padding | kCCOptionECBMode, key, kCCKeySize3DES ..,Chub
I got a compile error, it occurred the first line, var result:CCCryptorStatus = CCCrypt(kCCEncrypt... it tells 'Int' is not convertible to CCOperation. do you know how to solve it?Chub
I've been trying this with NMSSH, but I keep getting ld: library not found for -lNMSSH for architecture arm64 as well as for armv7. I can't figure out why.Ear
@Zaph, your statement could be rephrased as: Gee C makes it really hard to avoid security holes and bugs.Broadfaced
@porcoesphino Ah, Swift would have made Heartbleed impossible? Eliminate poor crypto practices? UnsafeBufferPointer is unnecessary? It is not the language, it is the programmers. Even the goto fail bug was mainly due to violating the DRY principle. Heaven help the person saying that "The Emperor has no clothes."Jacie
Kkk. You know my comment wasn't saying Swift was perfect. I've never coded in Swift but have coded a lot in C. Still it's easy to see that C allows developers to easily make mistakes that can be abused. Swift closes some of those holes. Apple likes closing holes (and pushing their agenda). It's annoying but hardly a surprise. What do they have to gain from doing what you're complaining about? They do have things to lose.Broadfaced
I'm wondering if Swift 1.2 allows an easier mechanism for doing this?Umber
For me it didn't work until I removed the link "CommonCrypto" from the module.map file.Baillie
Still necessary in Xcode 7 beta 5. However, note that it appears to be no longer necessary to include the setting in the project using the framework.Umber
@Mandolin This solution appears to no longer work in Xcode 7.3 beta 1. Struggling to find another solution. See: #34772739Umber
Can anyone confirm this working on Xcode 7.3? This solution stopped working for me after the update.Mould
Correnction: it's working fine when I build for simulator, but fails @ linking when I build for an iOS 9.3 device with "ld: library not found for -lCommonCrypto for architecture arm64"Mould
@OdedRegev I just followed the instructions in this answer and everything works for me now.Mould
It's working for me but out of curiosity... you only have a link to the simulator sdk but it's working on my device?!? how? :)Monoicous
There is so much BS with Apple XCode and Swift, I dont know where to start. Are you serious ? A absolute path in project setttings file, how in earth am I going to get a team to work on this ? They have to chnage the path everytime before they start work ? Really ? /Applications/Xcode6-Beta5.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator8.0.sdk/usr/include/CommonCrypto/CommonCrypto.h"Bolognese
L
52

This answer discusses how to make it work inside a framework, and with Cocoapods and Carthage

🐟 modulemap approach

I use modulemap in my wrapper around CommonCrypto https://github.com/onmyway133/arcane, https://github.com/onmyway133/Reindeer

For those getting header not found, please take a look https://github.com/onmyway133/Arcane/issues/4 or run xcode-select --install

  • Make a folder CCommonCrypto containing module.modulemap

      module CCommonCrypto {
        header "/usr/include/CommonCrypto/CommonCrypto.h"
        export *
      }
    
  • Go to Built Settings -> Import Paths

      ${SRCROOT}/Sources/CCommonCrypto
    

🌳 Cocoapods with modulemap approach

🐘 public header approach

🐏 Cocoapods with public header approach

🐝 Interesting related posts

Lacroix answered 9/5, 2016 at 21:28 Comment(5)
Wow! To my surprise, the header path from the top approach (creating the module.modulemap file) worked great when previously modulemap files had been causing lots of issues. I had been struggling with this for a while using a module.modulemap file with an absolute path to /CommonCrypto/CommonCrypto.h within Applications/XCode.app/Contents/Developer/Platforms/iPhoneOS.... , which required manual modification for people who renamed XCode. Switching that line to look at "/usr/include/CommonCrypto/CommonCrypto.h" seems to work fine for a team with several XCode versions. Thank you so much!Shogun
Im creating a pod, I set the SWIFT_INCLUDE_PATHS and preserve_paths. When I run pod lib lint, but build failed with error: no such module 'CommonCrypto'. How can I deal with it.Clerissa
Not related to the problem but I love the use of emoji as bullets! 😊Hervey
@Lacroix Using local development pod works if you replace $(PODS_ROOT)/CommonCryptoSwift/Sources/CCommonCrypto with $(PODS_TARGET_SRCROOT)/Sources/CCommonCrypto. PODS_TARGET_SRCROOT is set correctly for local pods.Dormancy
Great answer, saved my life! Thanks a millionSightseeing
M
46

Good news! Swift 4.2 (Xcode 10) finally provides CommonCrypto!

Just add import CommonCrypto in your swift file.

Mckelvey answered 5/6, 2018 at 0:9 Comment(10)
Great news! Can you add the link to documentation?Tinderbox
I don't have a link to documentation, I discovered this while trying to compile a project where I had one of the workarounds here with Xcode 10. It complained that it could find two CommonCrypto modules, thus suspecting Apple now provided I removed my workaround and ’lo! It was true. I tweeted about it and an Apple engineer replied confirming that it was intended.Mckelvey
App store is only showing me 9.4.7 as an available update, how you got Xcode 10?Melanism
It's in beta as a trivial Google search would have told you.Mckelvey
Awesome! Just do import CommonCrypto in your swift file.Hyperthyroidism
Thank you.. That's why I had got a message "redefinition of module 'commoncrypto'" with older projects when updated my xcode to 10.. I tried to figure out what's happening, then I saw your answerScutari
@COLDICE How did you solve the redefinition? Did you remove the previous import?Viceroy
@SomoyDasGupta yes. Just remove the previous import, and compile it again. In other words, you don't have to do the steps from MikeWeller answerScutari
thanks @COLDICE. But would it work with the previous iOS versions?Viceroy
@SomoyDasGupta yes, I believe so.Scutari
M
7

WARNING: iTunesConnect may reject apps that are using this method.


New member on my team accidentally broke the solution given by one of the top answers, so I decided to consolidate it in a small wrapper project called CommonCryptoModule. You can install it manually or via Cocoapods:

pod 'CommonCryptoModule', '~> 1.0.2'

Then, all you have to do is to import the module where you need CommonCrypto, like so:

import CommonCryptoModule

Hope someone else finds this useful.

Mould answered 3/11, 2017 at 13:56 Comment(2)
Warning: Your application will be rejected if you use this method!Hedvige
Yep, we got rejected as well, very weird, because we were uploading with no problems for several months using this method.Mould
P
6

For anyone using swift 4.2 with Xcode 10:

CommonCrypto module is now provided by the system, so you can directly import it like any other system framework.

import CommonCrypto

Point answered 3/2, 2019 at 7:18 Comment(1)
Duplicate of @mxcl's answer: https://mcmap.net/q/129191/-importing-commoncrypto-in-a-swift-frameworkSomniloquy
I
5

I think I have an improvement to Mike Weller's excellent work.

Add a Run Script phase before the Compile Sources phase containing this bash:

# This if-statement means we'll only run the main script if the
# CommonCrypto.framework 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

FRAMEWORK_DIR="${BUILT_PRODUCTS_DIR}/CommonCrypto.framework"

if [ -d "${FRAMEWORK_DIR}" ]; then
echo "${FRAMEWORK_DIR} already exists, so skipping the rest of the script."
exit 0
fi

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

ln -sf "${SDKROOT}/usr/include/CommonCrypto" "${FRAMEWORK_DIR}/Headers"

This script constructs a bare bones framework with the module.map in the correct place and then relies on Xcode's automatic search of BUILT_PRODUCTS_DIR for frameworks.

I linked the original CommonCrypto include folder as the framework's Headers folder so the result should also function for Objective C projects.

Insurer answered 9/5, 2018 at 15:25 Comment(1)
See dvdblk's answer for an improvement that covers usage in CocoaPods.Insurer
P
4

The modulemap solutions can be good, and are robust against SDK changes, but I've found them awkward to use in practice, and not as reliable as I'd like when handing things out to others. To try to make it all more foolproof, I went a different way:

Just copy the headers.

I know, fragile. But Apple almost never makes significant changes to CommonCrypto and I'm living the dream that they will not change it in any significant way without also finally making CommonCrypto a modular header.

By "copy the headers" I mean "cut and paste all of the headers you need into one massive header in your project just like the preprocessor would do." As an example of this that you can copy or adapt, see RNCryptor.h.

Note that all of these files are licensed under APSL 2.0, and this approach intentionally maintains the copyright and license notices. My concatenation step is licensed under MIT, and that only applies up to the next license notice).

I am not saying this is a beautiful solution, but so far it seems to have been an incredibly simple solution to both implement and support.

Pathetic answered 1/11, 2016 at 15:10 Comment(3)
I found this to compile reliably and it is plain simple. Does the application using the framework need to link to anything special, or is CommonCrypto always available?Candent
I think Security.framework is automatically linked (it's be a little while since a started a new project). If you get errors, that's the framework to link.Pathetic
This does seem like the most simple solution and it works great on one machine, but whenever I use the framework in another framework, or app, on another machine I get the 'missing module' error.Temikatemp
H
4

@mogstad has been kind enough to wrap @stephencelis solution in a Cocoapod:

pod 'libCommonCrypto'

The other pods available did not work for me.

Heartache answered 3/5, 2017 at 22:13 Comment(0)
R
2

I know this is an old question. But I figure out an alternative way to use the library in Swift project, which might be helpful for those who don't want to import framework introduced in these answers.

In Swift project, create a Objective-C bridging header, create NSData category (or custom class that to use the library) in Objective-C. The only drawback would be that you have to write all implementation code in Objective-C. For example:

#import "NSData+NSDataEncryptionExtension.h"
#import <CommonCrypto/CommonCryptor.h>

@implementation NSData (NSDataEncryptionExtension)
- (NSData *)AES256EncryptWithKey:(NSString *)key {
    //do something
}

- (NSData *)AES256DecryptWithKey:(NSString *)key {
//do something
}

And then in your objective-c bridging header, add this

#import "NSData+NSDataEncryptionExtension.h"

And then in Swift class do similar thing:

public extension String {
func encryp(withKey key:String) -> String? {
    if let data = self.data(using: .utf8), let encrypedData = NSData(data: data).aes256Encrypt(withKey: key) {
        return encrypedData.base64EncodedString()
    }
    return nil
}
func decryp(withKey key:String) -> String? {
    if let data = NSData(base64Encoded: self, options: []), let decrypedData = data.aes256Decrypt(withKey: key) {
        return decrypedData.UTF8String
    }
    return nil
}
}

It works as expected.

Rodmun answered 26/4, 2017 at 21:50 Comment(3)
This works very smoothly, and even allows you to keep internals internal (NSData+NSDataEncryptionExtension.h doesn't have to be public).Comparison
But what OS framework should I link against, to use this thing? Unlike others - I have to work with CommonCrypto in an Obj-C project, and that alone breaks on Xcode 9 with MacOS-10.13 SDKSaxe
@MottiShneor Link any OS from 10.9 or above. I am working on same environment and it works fine.Rodmun
P
2

I've added some cocoapods magic to jjrscott's answer in case you need to use CommonCrypto in your cocoapods library.


1) Add this line to your podspec:

s.script_phase = { :name => 'CommonCrypto', :script => 'sh $PROJECT_DIR/../../install_common_crypto.sh', :execution_position => :before_compile }

2) Save this in your library folder or wherever you like (however don't forget to change the script_phase accordingly ...)

# This if-statement means we'll only run the main script if the
# CommonCrypto.framework 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
FRAMEWORK_DIR="${BUILT_PRODUCTS_DIR}/CommonCrypto.framework"

if [ -d "${FRAMEWORK_DIR}" ]; then
echo "${FRAMEWORK_DIR} already exists, so skipping the rest of the script."
exit 0
fi

mkdir -p "${FRAMEWORK_DIR}/Modules"
echo "module CommonCrypto [system] {
    header "${SDKROOT}/usr/include/CommonCrypto/CommonCrypto.h"
    export *
}" >> "${FRAMEWORK_DIR}/Modules/module.modulemap"

ln -sf "${SDKROOT}/usr/include/CommonCrypto" "${FRAMEWORK_DIR}/Headers"

Works like a charm :)

Punishment answered 7/6, 2018 at 0:43 Comment(2)
Can you provide demo or sample framework project for the same along with the pod spec file?Hatch
@Punishment In my case I have a my own pod which uses third-party pod which uses CommonCrypto. Should I fix my pod or should it be fixed in that third-party pod?Maya
S
0

I'm not sure if something's changed with Xcode 9.2 but it's now much simpler to achieve this. The only things I had to do are create a folder called "CommonCrypto" in my framework project directory and create two files inside it, one called "cc.h" as follows:

#include <CommonCrypto/CommonCrypto.h>
#include <CommonCrypto/CommonRandom.h>

And another called module.modulemap:

module CommonCrypto {
    export *
    header "cc.h"
}

(I don't know why you can't reference header files from the SDKROOT area directly in a modulemap file but I couldn't get it to work)

The third thing is to find the "Import Paths" setting and set to $(SRCROOT). In fact you can set it to whatever folder you want the CommonCrypto folder to be under, if you don't want it at the root level.

After this you should be able to use

import CommonCrypto

In any swift file and all the types/functions/etc. are available.

A word of warning though - if your app uses libCommonCrypto (or libcoreCrypto) it's exceptionally easy for a not-too-sophisticated hacker to attach a debugger to your app and find out what keys are being passed to these functions.

Selfpossession answered 3/10, 2018 at 3:17 Comment(0)
D
0

In case you have the below issue :

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

In Xcode 10, Swift 4.0. CommonCrypto is a part of the framework.

Add

 import CommonCrypto

Remove

  • CommonCrpto lib file from link binary with libraries from Build phases
  • import CommonCrypto from Bridging header

This worked for me!

Declaim answered 1/11, 2018 at 11:34 Comment(0)
M
-1

It happened the same to me after updating Xcode. I tried everything I can do such as reinstalling cocoapods and cleaning the project, but it didn't work. Now it's been solved after restart the system.

Madder answered 25/8, 2019 at 23:25 Comment(0)
M
-14

It's very simple. Add

#import <CommonCrypto/CommonCrypto.h>

to a .h file (the bridging header file of your project). As a convention you can call it YourProjectName-Bridging-Header.h.

Then go to your project Build Settings and look for Swift Compiler - Code Generation. Under it, add the name of your bridging header to the entry "Objetive-C Bridging Header".

You're done. No imports required in your Swift code. Any public Objective-C headers listed in this bridging header file will be visible to Swift.

Matheson answered 3/3, 2015 at 15:19 Comment(3)
your method returns error: using bridging headers with framework targets is unsupportedSuitable
@Suitable You can't use a bridging header in a framework target, as the error suggests. The only option is to put it in the main framework header file, unfortunately.Creswell
If you had actually red the title of this thread you would have seen that the guy is wanting to import the CommonCrypto library into a Swift framework. You cannot use bridging headers in frameworks, and you cannot import the CommonCrypto framework into the umbrella header.Simian

© 2022 - 2024 — McMap. All rights reserved.