Saturday, December 27, 2014

Finally! CocoaPods with Swift!


UPDATE: As of version 0.36, Swift support has officially been added to CocoaPods.


Rejoice!

This is what we all have been waiting for! CocoaPods, our beloved dependancy manager for iOS and Mac projects finally supports Swift in their latest release 0.36. But hold your horses because 0.36 is still in beta. In this very short tutorial I'll show you how to give it a go. Mind you this is not a beginner's guide to CocoaPods. I assume you have have prior experience with using CocoaPods.

To use SwiftPods (I like the sound of that), first you need to install the latest version which is 0.36 beta of CocoaPods. Open up the terminal and type this.
 sudo gem install cocoapods --pre
This will install CocoaPods 0.36 beta. To confirm run the below command,
 pod --version  
It will output something like this 0.36.0.beta.1.

That's actually pretty much it. There are third party Swift libraries already supporting SwiftPods out there like EVCloudKitDao. You can include them in the Podfile and run pod install just like you would do in the good old days.

Troubleshooting

However when I first tried to use it, I hit a few bumps. Here I'm going to include the issues I came across and how I resolved them.

1. Even after successfully installing the 0.36 beta version, when you run pod --version you might see it outputs an old version (like 0.35). To resolve that I completely uninstalled CocoaPods using sudo gem uninstall cocoapods and reinstalled it again as I have shown you above.

2. If you're using third party libraries only written in Objective-C in a Swift project, when you install them using CocoaPods, you'll see them as static libraries instead of frameworks as it should. Otherwise you won't be able to use them in Swift.


 To resolve this you need to include the line use_frameworks! in your Podfile. Like so,
 source 'https://github.com/CocoaPods/Specs.git'  
 use_frameworks!  
 platform :ios, '8.0'  
 pod 'MagicalRecord/Shorthand'  
Now run pod install again and voilĂ !



That's it! Say bye bye to the hassle of manually dragging and dropping project files yadda yadda. Enjoy using CocoaPods in Swift projects!

5 comments:

  1. Nice Post. Keep it up :-)

    Sachin aka Swiftwala

    ReplyDelete
  2. thnks for this post

    ReplyDelete
  3. Hey my Pod version is 1.2.0 and i have created my own library in objective c code and set it up on cocoapod , i am installing my lib from cocoapod in swift project but i am not able to use it in swift file. i have use use_frameworks! flag in my pod file as well.
    any idea what could be the issue ?

    ReplyDelete