30 January 2015

Using Objective-C Pods In Swift Project

Currently, Swift is not yet widely used for developing iOS application. Whether swift will replace Objective-C in a whole is still questionable. However, for current version of Swift, it has capability to import Objective-C code in Swift and vice versa.

A lot of useful library for iOS, registered at, written in Objective-C. Absolutely rewriting all of the libraries is neither a pleasant thing nor efficient. So, what we want to do is importing those libraries into our Swift project and I want to share to do ‘that’ in this article.

1. Prerequisites

When writing this article, I am using CocoaPods 0.33.1, and Xcode 6.1. A lot of Cocoapods materials (about what is it, how to install it) can found at CocoaPods Guide. In this article I will give a simple example using MFSideMenu. Let’s get started!

2. Let’s Make Swift Project

It is pretty straight forward to make a Swift based project. Just open your Xcode and create a new project, choose single view application for iOS, type your product name, and choose Swift as your programming language.

Screen Shot 2015-01-30 at 2.15.15 PM


3. Add Pods Library To Your Project


After that, add a text file named “Podfile” to your project directory and code below to Podfile:

platform :ios, '7.0'

pod 'MFSideMenu'

Open your terminal, go to your project directory then run “pod install“. After your Pods installed successfully, close the Xcode project then use *.xcworkspace file to open your project. Yet we still can’t use library in your Pods. To make MFSideMenu to be recognised in your Swift code, we have to make ‘bridging’ file. The file is a C header file and obligated to use pattern “*-Bridging-Header.h” as its file name, for example I use “SSB-Bridging-Header.h” for my bridging file. Add these lines to your bridging file:

#ifndef Swift_With_Side_Bar_SSB_Bridging_Header_h

#define Swift_With_Side_Bar_SSB_Bridging_Header_h

#import "MFSideMenu/MFSideMenu.h"


Let’s register this file to your Xcode. Go to Build Settings -> Objective-C Bridging Header and type the path of the bridging file, for best practice use $(SRCROOT) as project directory path, so we can easily add the remaining part of the full path as shown in below example:

Screen Shot 2015-01-30 at 2.44.11 PM


4. Use Pods Library In Your Swift Code

Open your AppDelegate.swift and replace all generated code with codes below:

import UIKit


class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

self.window = UIWindow(frame:UIScreen.mainScreen().bounds)


var leftVC:UIViewController = UIViewController();

leftVC.view.backgroundColor = UIColor(white: 1.0, alpha: 0.5)


var centerVC:UIViewController = UIViewController();

centerVC.view.backgroundColor = UIColor.whiteColor();

var navVC:UINavigationController = UINavigationController(rootViewController: centerVC)


var sideMenu:MFSideMenuContainerViewController = MFSideMenuContainerViewController.containerWithCenterViewController(navVC, leftMenuViewController: leftVC, rightMenuViewController: nil)


self.window?.rootViewController = sideMenu;


return true



5. Author Hates Storyboard:)

Previous section show us the whole code we need, however we cannot build and run this code yet. We need a little more configuration so instead of using story board, we use only appDelegate.swift to run this project. Open your file and remove “Main storyboard file base name”. Then built and run it on your iOS simulator, and swipe from left to right open the sidebar :).

Screen Shot 2015-01-30 at 3.27.20 PM

Latest posts by Ibnu Sina (see all)