Importing CommonCrypto in a Swift framework

109,295

Solution 1

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.

Solution 2

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

Solution 3

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.

Solution 4

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

Solution 5

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

Just add import CommonCrypto in your swift file.

Share:
109,295

Related videos on Youtube

hpique
Author by

hpique

iOS, Android &amp; Mac developer. Founder of Robot Media. @hpique

Updated on August 26, 2020

Comments

  • hpique
    hpique over 3 years

    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.

    • rmaddy
      rmaddy almost 10 years
      CommonCrypto is a C-based framework, not an Objective-C framework.
    • hpique
      hpique almost 10 years
      @rmaddy Objective-C is a C superset. Are you saying we can't use CommonCrypto from Swift?
    • hpique
      hpique almost 10 years
      @rmaddy I just managed to get CommonCrypto working by using module maps. I will polish the solution and post it later today.
    • Marcin
      Marcin over 9 years
      if you find it convenience, and what you looking for is already implemented, you can give a try to CryptoSwift
    • eyeballz
      eyeballz over 8 years
      Apple just open sourced CommonCrypto. Maybe we can get it running if we have the sources.
    • Roy Falk
      Roy Falk over 6 years
      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.
    • Martin Berger
      Martin Berger about 6 years
      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.
  • zaph
    zaph over 9 years
    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?
  • danimal
    danimal over 9 years
    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.
  • Saorikido
    Saorikido over 9 years
    I got a compile error, var result:CCCryptorStatus = CCCrypt( kCCEncrypt, kCCAlgorithm3DES, kCCOptionPKCS7Padding | kCCOptionECBMode, key, kCCKeySize3DES ..,
  • Saorikido
    Saorikido over 9 years
    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?
  • Garrett
    Garrett over 9 years
    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.
  • Bodey Baker
    Bodey Baker over 9 years
    @Zaph, your statement could be rephrased as: Gee C makes it really hard to avoid security holes and bugs.
  • zaph
    zaph over 9 years
    @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."
  • Bodey Baker
    Bodey Baker over 9 years
    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.
  • A . Radej
    A . Radej over 9 years
    I'm wondering if Swift 1.2 allows an easier mechanism for doing this?
  • gorgi93
    gorgi93 about 9 years
    your method returns error: using bridging headers with framework targets is unsupported
  • Charles A.
    Charles A. about 9 years
    @gorgi93 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.
  • stannie
    stannie about 9 years
    Works for me without step (5). With it I get a build error: ld: cannot link directly with /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneS‌​imulator.platform/De‌​veloper/SDKs/iPhoneS‌​imulator8.2.sdk/usr/‌​lib/system/libcommon‌​Crypto.dylib. Link against the umbrella framework 'System.framework' instead. for architecture x86_64
  • stephencelis
    stephencelis about 9 years
    @stannie 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).
  • Sam Soffes
    Sam Soffes about 9 years
    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.
  • Teo Sartori
    Teo Sartori almost 9 years
    For me it didn't work until I removed the link "CommonCrypto" from the module.map file.
  • dogsgod
    dogsgod almost 9 years
    Amazing. Step 5 is not working for me either, but without - like a charm! I owe you a beer or two!
  • A . Radej
    A . Radej almost 9 years
    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.
  • hola
    hola over 8 years
    How or where do people find out stuff like this?
  • Teo Sartori
    Teo Sartori over 8 years
    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.
  • Vasily
    Vasily over 8 years
    Getting "no such module CommonCrypto" always… Also doesn't have .dylib in step 5. XCode 7.2
  • A . Radej
    A . Radej over 8 years
    @hpique This solution appears to no longer work in Xcode 7.3 beta 1. Struggling to find another solution. See: stackoverflow.com/questions/34772239/…
  • Danny Bravo
    Danny Bravo over 8 years
    This solution works on our umbrella project but the tests this are failing with a "No such module: 'CommonCrypto'" error.
  • Anton Tropashko
    Anton Tropashko over 8 years
    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 mess
  • Anton Tropashko
    Anton Tropashko over 8 years
    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 masochists
  • miken.mkndev
    miken.mkndev about 8 years
    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.
  • Reda Lemeden
    Reda Lemeden about 8 years
    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.
  • Nikita Kukushkin
    Nikita Kukushkin about 8 years
    Can anyone confirm this working on Xcode 7.3? This solution stopped working for me after the update.
  • Nikita Kukushkin
    Nikita Kukushkin about 8 years
    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"
  • Oded Regev
    Oded Regev about 8 years
    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 Bitcode
  • Nikita Kukushkin
    Nikita Kukushkin about 8 years
    @OdedRegev I just followed the instructions in this answer and everything works for me now.
  • Adam Carter
    Adam Carter about 8 years
    Trying to build with this method causes build errors using Xcode 7.3
  • Simon Guerout
    Simon Guerout about 8 years
    Can't make it work at all, it won't build except when running a test on my machine.
  • Oded Regev
    Oded Regev about 8 years
    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"
  • picciano
    picciano almost 8 years
    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.
  • eyeballz
    eyeballz almost 8 years
    Update: I asked some Apple engineers yesterday and they said modulemap is the way to go.
  • Roy Falk
    Roy Falk almost 8 years
    Note that you must download the command line tools or none of the above will work. xcode-select --install will solve the problem.
  • kailoon
    kailoon almost 8 years
    Not sure if this is a good solution, but I was able to build and run on my device. stackoverflow.com/questions/38800435/…
  • Benjohn
    Benjohn over 7 years
    Is it possible to replace at least some of /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneS‌​imulator.platform/De‌​veloper/SDKs/iPhoneS‌​imulator.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!
  • DShah
    DShah over 7 years
    I am using Xcode 8.2.1, and I do not find "libcommonCrypto.dylib" mentioned in step 5. What is the alternative to that?
  • jamil ahmed
    jamil ahmed over 7 years
    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?
  • Matthew Seaman
    Matthew Seaman over 7 years
    @i_am_jorf You should be able to use "MacOSX.sdk" instead of "MacOSX10.11.sdk" in the path.
  • codingFriend1
    codingFriend1 about 7 years
    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?
  • Rob Napier
    Rob Napier about 7 years
    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.
  • Georg
    Georg about 7 years
    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? :)
  • jamil ahmed
    jamil ahmed almost 7 years
    @HuseinBehboodiRad because you can't use a bridging header in a swift framework.
  • Abdullah Saeed
    Abdullah Saeed almost 7 years
    This answer should be on top. Simple and elegant
  • Raphael
    Raphael almost 7 years
    This works very smoothly, and even allows you to keep internals internal (NSData+NSDataEncryptionExtension.h doesn't have to be public).
  • fatuous.logic
    fatuous.logic almost 7 years
    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.
  • Natalia
    Natalia almost 7 years
    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!
  • Klein Mioke
    Klein Mioke almost 7 years
    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.
  • Ravi Kiran
    Ravi Kiran almost 7 years
    If I do this in my .framework , should I have to do the same in the projects where I include this framework?
  • richever
    richever almost 7 years
    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.
  • richever
    richever almost 7 years
    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.
  • Mike Weller
    Mike Weller almost 7 years
    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?
  • Fomentia
    Fomentia over 6 years
    Not related to the problem but I love the use of emoji as bullets! 😊
  • Vanya
    Vanya over 6 years
    @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.
  • Vanya
    Vanya over 6 years
    Found that. Wrong reference for module.
  • Krishnarjun Banoth
    Krishnarjun Banoth over 6 years
    @Vanya did you success with xCode9.
  • Vanya
    Vanya over 6 years
    @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.
  • Krishnarjun Banoth
    Krishnarjun Banoth over 6 years
    @Vanya ok, let me know results of your doing.
  • Ian Dundas
    Ian Dundas over 6 years
    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 :(
  • HaneTV
    HaneTV over 6 years
    @Vanya : 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." ?
  • iwasrobbed
    iwasrobbed over 6 years
    @IanDundas I updated the code above with a fix for the re-compilation issue as well as a fix for using this on macOS
  • Orkhan Alikhanov
    Orkhan Alikhanov over 6 years
    @onmyway133 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.
  • Ky -
    Ky - over 6 years
    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/S‌​DKs/MacOSX.sdk/usr/i‌​nclude/CommonCrypto/‌​CommonCrypto.h
  • Segabond
    Segabond over 6 years
    Warning: Your application will be rejected if you use this method!
  • Nikita Kukushkin
    Nikita Kukushkin over 6 years
    Yep, we got rejected as well, very weird, because we were uploading with no problems for several months using this method.
  • Siddharth
    Siddharth over 6 years
    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.plat‌​form/Developer/SDKs/‌​iPhoneSimulator8.0.s‌​dk/usr/include/Commo‌​nCrypto/CommonCrypto‌​.h"
  • Abhijeet Barge
    Abhijeet Barge about 6 years
    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?
  • Mike Weller
    Mike Weller about 6 years
    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.
  • Mike Weller
    Mike Weller about 6 years
    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 frameworks
  • Motti Shneor
    Motti Shneor about 6 years
    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 SDK
  • Terence
    Terence about 6 years
    @MottiShneor Link any OS from 10.9 or above. I am working on same environment and it works fine.
  • Max
    Max about 6 years
    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!
  • Motti Shneor
    Motti Shneor about 6 years
    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 automatically
  • Hassan Shahbazi
    Hassan Shahbazi almost 6 years
    Great answer, saved my life! Thanks a million
  • Kryštof Matěj
    Kryštof Matěj almost 6 years
    Great news! Can you add the link to documentation?
  • mxcl
    mxcl almost 6 years
    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.
  • Hammad Tariq
    Hammad Tariq almost 6 years
    App store is only showing me 9.4.7 as an available update, how you got Xcode 10?
  • mxcl
    mxcl almost 6 years
    It's in beta as a trivial Google search would have told you.
  • jjrscott
    jjrscott almost 6 years
    See dvdblk's answer for an improvement that covers usage in CocoaPods.
  • SafeFastExpressive
    SafeFastExpressive over 5 years
    A little more love for @MikeWeller and his timely update on supporting Xcode 10!
  • Gowtham
    Gowtham over 5 years
    Can you provide demo or sample framework project for the same along with the pod spec file?
  • Gowtham
    Gowtham over 5 years
    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?
  • Baran Emre
    Baran Emre over 5 years
    You are awesome! That just saved us a ton of time. Thanks!
  • Mohammad Zaid Pathan
    Mohammad Zaid Pathan over 5 years
    Awesome! Just do import CommonCrypto in your swift file.
  • COLD ICE
    COLD ICE over 5 years
    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 answer
  • Somoy Das Gupta
    Somoy Das Gupta over 5 years
    @COLDICE How did you solve the redefinition? Did you remove the previous import?
  • COLD ICE
    COLD ICE over 5 years
    @SomoyDasGupta yes. Just remove the previous import, and compile it again. In other words, you don't have to do the steps from MikeWeller answer
  • Somoy Das Gupta
    Somoy Das Gupta over 5 years
    thanks @COLDICE. But would it work with the previous iOS versions?
  • COLD ICE
    COLD ICE over 5 years
    @SomoyDasGupta yes, I believe so.
  • Lohith Korupolu
    Lohith Korupolu over 5 years
    Awesome! I think this should be the accepted answer!!
  • Rainer Schwarz
    Rainer Schwarz over 5 years
    Duplicate of @mxcl's answer: stackoverflow.com/a/50690346/9988514
  • Vyachaslav Gerchicov
    Vyachaslav Gerchicov almost 4 years
    @dvdblk 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?