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.
Added Bridging Header:
3. Setup Pod with your project.
4. Add your Pod libraries inside bridging header file as you were normally using in Objective C project.
6. Now you can access MBProgressHUD methods in your swift like:
MBProgressHUD.showHUDAddedTo(self.view, animated: true)
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: email@example.com Skype: muz.iphonedev