How to use CocoaPods with Swift project

If you already know how to install and use CocoaPods then continue else please first visit my previous blog “How to install CocoaPods in XCode” (for Objective-C).

Using CocoaPods or any Objective C library in Swift is not a big deal. XCode is quite smart and it automatically do this for you. You can also check Apple’s documentation on this Using Swift with Cocoa and Objective-C. Once libraries are added, you only need to use a bridging header between Objective-C and Swift and use the new coding style of Swift for calling those methods. Like [MBProgressHUD show] become MBProgressHUD.show( ) etc. Here I am using the example of MBProgressHUD because it is normally used in most of the projects.

Let’s see how it works:

1. Create a new XCode project and select the language Swift (no need to to this if you already have Swift project).

2. Create a Bridging-Header.h file starting with your project name. like, if my project name is SampleProject, bridging header become: SampleProject-Bridging-Header.h and Put this header at the root directory of your project.

If you are not very sure how to do this, the most simplest way is to add a test objective-c file in your project (remove that later). Xcode will prompt an option to configure bridging. When you press Yes, XCode automatically create the bridging file and put inside root of project directory.

bridging

Added Bridging Header:

bridging-file

3. Setup Pod with your project.

4. Add your Pod libraries inside bridging header file as you were normally using in Objective C project.

pod-files

6.  Now you can access MBProgressHUD methods in your swift like:

        MBProgressHUD.showHUDAddedTo(self.view, animated: true)
        MBProgressHUD.hideAllHUDsForView(self.view, animated:true)

xcode-swift

You can download my SampleProject from git. While reading this if you notice that step5 is missing, then cheers!! you follow this carefully and you understand how it works 🙂

Still facing any issue with Pod? Or want to be in touch with us? Contact us:

Email:  muzammil@triffort.com Skype: muz.iphonedev

Triffort_logo

Triffort Technologies Pvt Ltd
www.triffort.com

This entry was posted in iOS, iPhone | iPad, Technology and tagged , , , , , , by Muzammil. Bookmark the permalink.

About Muzammil

WHO I AM? Hi, I am Muzammil Mohammad. I am here to share my knowledge with all mobile application developers and those companies who are stuck on development (due to bad quality of code or don't know what to do) or want to know how to build a good mobile app. I believe I am more expert in UI and Architecture design of mobile app. So if you need my helps or assistant for your mobile app projects, Feel free to contact me. I will try my best to improve the quality of your app. Or you can hire me to develop your apps. I’d love to talk with you. Best Regards

2 thoughts on “How to use CocoaPods with Swift project

Leave a Reply

Your email address will not be published. Required fields are marked *