83

As Apple introduced Swift, their new programming language, I wonder how you can integrate it with existing Objective-C libraries that are available via CocoaPods?

1
  • You can also read my recent article on how to setup CocoaPods with your Swift project. Commented May 26, 2017 at 14:24

6 Answers 6

78

Cocoapods 0.36 and above introduces the use_frameworks! instruction which implies that the bridging header is not required for importing Objective-C pods in Swift.

Please find below a full example using MBProgressHUD and Alamofire:

1. Podfile

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.3'
use_frameworks!

pod 'Alamofire', '>= 1.2.2' # Swift pod
pod 'MBProgressHUD', '>= 0.9.1' # Objective-C pod

2. Deletion

Remove the #imports from your bridging header or even delete the bridging header file if you do not need it. If you choose the latter possibility, do not forget to delete the path (to this deleted bridging header file) in your Xcode project configuration.

3. Adding imports

Add import MBProgressHUD and/or import Alamofire at the top of every Swift files that need these class(es).

4. Fix the enums if necessary

You're now using bona fide frameworks, so your enums have moved in flight! You might have a line of Swift that was fine with the bridging header like this:

progressHUD.mode = MBProgressHUDModeIndeterminate

That now has to become this:

progressHUD.mode = MBProgressHUDMode.Indeterminate

Not to big a deal, but the pile of errors might lead you astray that you have a bigger problem than you do if you are using a lot of Objective-C enums.

(Source of this answer)

For your information: I guess (you will have to test by yourself to be sure) that the use_frameworks! instruction in your Podfile is only compatible with Xcode projects targeting iOS >= 8.

Sign up to request clarification or add additional context in comments.

Comments

73

It seems that the process is similar to the one described in Mix and Match section of Using Swift with Cocoa and Objective-C documentation.

  1. Create your Podfile and run pod install.
  2. Create a new Objective-C header file, Example-Bridging-Header.h, and add it to the project.
  3. Add import statement to the bridge header.
  4. Set Objective-C Bridging Header for your target:

enter image description here

Now you can use your library, in that case, MKUnits, in your Swift file:

let kilograms = NSNumber.mass_kilogram(2)()
let pounds = NSNumber.mass_pound(10)()
let result = kilograms.add(pounds)
println(result)

More here: Integrating Cocoapods with a Swift project

8 Comments

For me the import statement needed to include the project dir, aka #import <Reachability/Reachability.h> rather than #import "Reachability.h".
Just an update. Swift PODs, ie: Swift Libs will also be soon supported by CocoaPods. Next version 0.36 will bring the full Swift support in CocoaPods. Most of the feature is already complete and you can test-drive the Swift Pods right now using the instructions from my blog-post
Cocoapods 0.36 and above introduces the use_frameworks! instruction which implies that the bridging header is not required anymore for importing Objective-C pods in Swift (please see my answer below).
In addition to the above, I found that I had to add "Pods/**" to my "User Header Search Paths" in Build Settings. I also had to add "#import<Foundation/Foundation.h>" to the bridge file.
Thank you Swifty. I tried use_frameworks! but it didn't work for me. Adding "Pods/**" to the user search path worked.
|
16

UPDATE: CocoaPods 0.36 stable version has been released. It officially supports Swift.


CocoaPods now supports Swift in their latest 0.36 release. It's still in beta but it works.

First you need to install the CocoaPods beta (currently beta 2) by running this in your Terminal.

sudo gem install cocoapods --pre

That's pretty much it. You can add Swift libraries like you would do normally.

But there's a catch if you want to add a library written in Objective-C to a Swift project via CocoaPods. You need to add the line use_frameworks! to your Podfile. Here's an example.

use_frameworks!
platform :ios, '8.0'

pod 'MagicalRecord'

I wrote a blog post regarding this as well.

1 Comment

use_frameworks! is only for iOS >= 8 right? If so you should write this in your answer.
2

If you are getting a file not found error in your bridging-header.h, you might want to make sure your Pods library is being linked in your Build Scheme.

The CocoaPods troubleshooting section describes how to do this under item #4 here

Comments

1

Now you can use cocoapods 0.36.0 version by running sudo gem install cocoapods which supports to integrate swift frameworks . When you use a framework written by swift, you should explicit use it in Podfile:

platform :ios, '8.0'
use_frameworks!
pod 'Alamofire'

Comments

0

In my case, I found that I was not adding pods to main target but to only Test and TestUI targets in Podfile as explained here.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.