86

Is there a way I can use a CocoaPod written in Objective-C in my Swift project using swift?

Do I just make a bridging header? And if so, can I access the objects, classes, and fields defined by the libraries in the CocoaPod in Swift?

jscs
  • 63,694
  • 13
  • 151
  • 195
shaydawg
  • 1,193
  • 1
  • 12
  • 17

4 Answers4

175

Basic answer to your question is Yes, you can use objective-c code built with CocoaPods.

More important question is "How to use such libs?"
Answer on this question depends on use_frameworks! flag in your Podfile:
Let's imagine that you want use Objective-C pod with name CoolObjectiveCLib.

If your pod file uses use_frameworks! flag:

// Podfile
use_frameworks!
pod 'CoolObjectiveCLib'

Then you don't need add any bridge header files.
Everything that you need is import framework in Swift source file:

// MyClass.swift
import CoolObjectiveCLib

Now you can use all classes that are presented in lib.

If your pod file doesn't use use_frameworks! flag:

// Podfile
pod 'CoolObjectiveCLib'

Then you need create bridging header file and import there all necessary Objective-C headers:

// MyApp-Bridging-Header
#import "CoolObjectiveCLib.h"

Now you can use all classes that are defined in imported headers.

Vlad Papko
  • 13,184
  • 4
  • 41
  • 57
  • how to access swift classes from inside pod .m files (in case i want to customise these pods) ? – JAHelia Mar 21 '16 at 06:03
  • 1
    I had use_frameworks uncommented in my swift project's podfile and wanted to use the Toast pod, which actually is an objective-c category. By simply writing import UIView+Toast gave me compilation error: Consecutive statements on a line must be separated by ';' – Chanchal Raj Jun 14 '16 at 11:06
  • @ChanchalRaj is `UIView+Toast` name for your pod? If not try to use import framework which name matches to your pod name. – Vlad Papko Jun 14 '16 at 14:51
  • @Visput The name of the pod is Toast, tried it too, but didn't work. Here is the github link: https://github.com/scalessec/Toast – Chanchal Raj Jun 15 '16 at 04:16
  • @ChanchalRaj Just tried and it works fine. Steps I did: 1. Add `pod 'Toast'` to my pod file. 2. Updated pods. 3. Add `import Toast` to swift file where I want to use this lib. 4. Create view `let view = UIView()`. 5. Call lib method: `view.makeToast("My message")` – Vlad Papko Jun 15 '16 at 18:28
  • @Visput, thanks for trying. Did u create a swift project? As mentioned by me earlier I had a swift project and wanted to use this objective-c pod. – Chanchal Raj Jun 16 '16 at 06:04
  • @ChanchalRaj, I used existing swift project, and I tried it with exactly that pod that you referenced. Just try my steps, probably you missed something. – Vlad Papko Jun 16 '16 at 06:26
  • @AbhishekThapliyal What exactly doesn't work? Or what error do you see? – Vlad Papko Mar 03 '17 at 17:53
  • 1
    I have used same code to use objc code to swift file but it gives me error no such a module "POD NAME" , Do I need to set any other flag – Prashant Tukadiya May 01 '17 at 05:25
  • @MikeAlter Could you show how your pod file looks like? – Vlad Papko May 01 '17 at 14:23
  • @VladPapko I found the solution , I am using static library so it was not working , so I found solution that I have to use Umbrella bridging – Prashant Tukadiya May 02 '17 at 05:25
  • 5
    If you are facing issues with **use_frameworks!**, it might be because the import statements are not written in the correct format. e.g. if the podFile contains `pod 'iOS-Echarts'`, then imports shall happen like following: _Swift file_: `import iOS_Echarts` _Objective-C file_: `#import ` – Kushal Ashok Aug 18 '17 at 08:48
  • For me your answer clear all confusions.. Thanks Man – Syed Nazar Muhammad Jun 02 '18 at 15:58
  • These steps do not work if you are working in a CocoaTouch framework :'( – Diego Palomar Jan 03 '19 at 22:16
3

In podFile use the flag use_frameworks! Inside Xcode in the Pod folder structure in the dependency, you add xxxxxxx-umbrella.h in Support Files.

In your {PROJECT_NAME}-Bridging-Header.h use:

#import "xxxxxxx/xxxxxxx-umbrella.h"

It works for me.

Unheilig
  • 16,196
  • 193
  • 68
  • 98
RodolfoNeto
  • 483
  • 5
  • 4
0

You just need a bridging header and import there what you need.

Lucian Boboc
  • 480
  • 2
  • 6
0

AND don't forget to add Bridging Header file name to Target -> Build Settings -> Objective-C Bridging Header

o0sea0o
  • 191
  • 2
  • 5