Uh. XCode 6 is out in the developer preview version and Apple introduced Swift on the WWDC. We all love CocoaPods, but how to use the Pods in a new Swift project?

Let’s start with a normal Podfile for a test project called ‘cocoapods-test’

platform :osx, '10.10'
link_with 'cocoapods-test', 'cocoapods-testTests'
pod 'RaptureXML'

As you can see, I link also the test-target in my Podfile, because I want the Pods to also be available there.

Now lets execute the beloved

pod install
Successful included Pods
Successful included Pods

But now? We have a pure Swift project here, so how to I use the Objective-C resources in my target? The solution is already mentioned in the official Swift guide:

Fill out an Objective-C bridging header if you need to access Objective-C code from the same app target in your Swift file.

We need a bridging header, which Xcode automatically creates when you want to add the first Objective-C file to a Swift project. So we just add a file called “dummy”. This file I will delete later.

Yes, we want an objective C file now
Yes, we want an objective C file now

Then Xcode will ask

This is exactly what we want right now. A nice bridging header file.
This is exactly what we want right now. A nice bridging header file.

This auto-generation will also add the corresponding path to this header file into our Build Settings.

Build Settings for the Bridging-Header file
Build Settings for the Bridging-Header file

Into this header file we can then add our wanted Objective C headers like e.g.

#import "RXMLElement.h"

We can the use this library in our Swift files like e.g.

var rootXML : RXMLElement! = RXMLElement.elementFromXMLData(data) as RXMLElement
  • Ross Kimes

    Have you been able to get this to work with the new Frameworks? I am trying to build a Swift framework that depends on a CocoaPod, but when I build the Framework the libPods does not seem to be built with it.

  • http://classictutorials.com Kaushik

    There are another thousands of tutorial for this. But this is the most detailed and talks about the right steps. Thanks. Works perfectly.

  • WP

    CocoaPods works perfectly well. But some Frameworks do not. I tested “Parse.com” and “AWS”. Both made the Swift editor act up and stop working.

    Has anyone else found this?