How to use Objective-C Cocoapods in Swift Project?

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

Am I just making a headline? And if so, can I access the objects, classes, and fields defined by the libraries in CocoaPod in Swift?

There are many good CocoaPods written in Objective C. I only know Swift, and I am wondering if I have a way to use these CocoaPods.

+59
ios objective-c swift cocoapods
Aug 07 '15 at 18:35
source share
3 answers

The main answer to your question: yes, you can use objective-c code created using CocoaPods.

More important question: "How to use such libraries?" The answer to this question depends on the use_frameworks! flag use_frameworks! in your Podfile :
Imagine you want to use objective-c pod named CoolObjectiveCLib .

If your pod file uses the use_frameworks! flag use_frameworks! :

 // Podfile use_frameworks! pod 'CoolObjectiveCLib' 

Then you do not need to add bridge header files.
All you need is an import framework in the Swift source file:

 // MyClass.swift import CoolObjectiveCLib 

Now you can use all the classes provided in lib.

If your pod file does not use the use_frameworks! flag use_frameworks! :

 // Podfile pod 'CoolObjectiveCLib' 

Then you need to create a bridge header file and import there all the necessary objective-c headers:

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

Now you can use all classes defined in the imported headers.

+121
Aug 07 '15 at 19:27
source share
— -

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

0
Aug 07 '15 at 18:37
source share

In podFile use the use_frameworks flag! Inside Xcode, in the Pod folder structure, depending, you add xxxxxxx-umbrella.h to the support files.

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

 #import "xxxxxxx/xxxxxxx-umbrella.h" 

This works for me.

0
May 23 '17 at 17:31
source share



All Articles