Import Objective C header from Pod? Easy peasy!
I am a new iOS programmer. I come 3 years after Swift so my first language in iOS programming is Swift. While there are a lot of libraries outthere which can help me to develop an app, its not as easy as a Swift library which seamlessly just import its module after “pod install” completed.
For more clarity, here I am talking about how to import an Objective C header from a library which injected through Cocoapods. Its easy to inject a library to a Swift project manually. You could just drag and drop its file from external folder to your project and Xcode will give you option to generate an Objective C Bridging Header for you. Or you can make a new Objective C header file “.h” and name it as follows; <#ProjectName#>-Bridging-Header.h
Before we go, I want to make this to be a simple tutorial, not just a way experienced programmer remember something. Which means, altough the implementation is short, one line code
, but I want to share how I understand this from trial and error.
So, as usual, lets make a new Xcode project. Name it as you like, ignore Core Data, UI Test, and Unit Test. Just like you do everyday, if you make a new app every day.
Then I need you to at leas know how to use Cocoapod. If you don’t know what Cocoapod is, then take a little amount of time to learn about that such amazing tool iOS developer community provide to us. You will want to go to its official website which also provide the way we find our desired library. Go to http://cocoapods.org
New I assume you get the grasp of Cocoapod already. Then we will make our project to use external library which will be fetched from Cocoapod. Go to project root folder, if you don’t know what root folder is, then just go to a folder which contains our <#Project#>.xcodeproj file.
Run pod init
on terminal.
Once initalize pod is done, lets open Podfile
to declare which pod library to import.
Lets find a good library which only written in Objective C. I mean, this library have no counterpart, or not yet, in Swift. We have MagicalRecord which will help us to manage CoreData usage.
Declare MagicalRecord to Podfile by put pod 'MagicalRecord','~> 2.3'
under #Pods from … line.
Save Podfile then run pod install
on terminal. We will have a bunch of log which sent by pod to inform current action in our project. Wait till finish.
Now we have <#ProjectName#>.xcworkspace along with .xcodeproj in root folder. If we are using Cocoapod as third library manager, we will want use .xcworkspace each time working on our project. If you want to look an alternative to Cocoapod, we have Carthage and Swift Package Manager as option.
Open .xcworkspace. Our project remain the same except we have two project registered in Xcode navigator pane. One is our own project, the second is Pod project. This is why we need to use xcworkspace in order to load Pod project always.
Now lets build the project before going on. Why we need to do this? To my self, I want to be convinced that the project is not broken by showing red error.
So far we doing above steps, but the library is Objective C while the project is in Swift. How to use it on our project? Yes, we need to use Bridging Header.
Lets make Objective C header file and name it as I wrote in the first paragraph. <#ProjectName#>-Bridging-Header.h
What to do next?
Lets import our MagicalRecord into project via Bridging Header so Xcode will have a way to implicitly convert interfaces to be called in Swift way.
#import <MagicalRecord/MagicalRecord.h>
That’s it. You’ve done all steps to use an Objective C library which fetched from Cocoapod in Swift project. I recommend you to build project, and if some error happen, just delete Derived Data, clean CMD+SHIFT+K
, and analyze CMD+SHIFT+B
your project. Hope it helps.
Next we will have more advanced topics, stay tuned!