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

How to install CocoaPods and setup with your Xcode project

Sometimes we are using many Git libraries inside our app. Like JSONKit, AFNetworking,  MBProgressHUD, EGOTableViewPullRefresh, Facebook-iOS-SDK etc. But it is very boring to build all libraries or if you add the code in your project, it is tough to manage. Also, there are many disadvantages in this:

  • Code that could be somewhere else is stored in your repository, wasting space.
  • Sometimes, it’s hard to get a specific version of a library.
  • There’s no central place where you can see which libraries are available.
  • Finding a new version of a library and updating your project with it is time taking and sometimes, painful.
  • Downloading libraries manually creates a tendency to perhaps make some changes to the downloaded code.
  • Updation is harder.

The most simplest solution is to use CocoaPods.

CocoaPods is the dependency manager for Objective-C and Swift projects. It has thousands of libraries and can help you scale your projects elegantly. CocoaPods is built with Ruby and is installable with the default Ruby available on OS X.

Installation:

Before installing Pods make sure you have installed/updated command line tool in your Mac machine. You can check this by opening Xcode, navigating the menu to Xcode > Preferences > Downloads > Components, finding Command Line Tools and select install/update.

command-line-tools

Also, make sure Git is installed in your Mac machine.

Open terminal and run this command:

sudo gem install cocoapods

Enter admin password. This could take a while. After few minutes it will show green message is cocoa pods installed successfully in your mac machine.

If you are getting any error with XCode like developer path is missing. First run this command in terminal: sudo xcode-select -switch /Applications/Xcode.app (or your XCodeName.app)

Hurrah….You successfully installed CocoaPods in your mac machine. Now you can setup Pod with your Xcode project.

How to setup POD for your Xcode project:

1. Open Terminal

2. Change directory to your XCode project root directory (where your ProjectName.xcodeproj file is placed).

3. $ pod setup : (Setting up CocoaPods master repo)

If successful, it shows : Setup completed (read-only access). So, you setup everything. Now Lets do something which is more visible…Yes ! Lets install libraries in your Xcode project.

Steps to add-remove-update libraries in pod:

1. Open Terminal

2. Change directory to your XCode project root directory. If your terminal is already running then no need to do this, as you are already at same path.

3. $ touch podfile

4. $ open -e podfile (This should open a blank text file or you can use: open -a Xcode podfile to open this inside Xcode)

5. Add your library names in that text file. You can add new names (lib name), remove any name or change the version. If you are using CocoaPods 1.0 or above you also have to have a ‘target’ section in your Pod File.

target '<Your Target Name>' 
do 
 pod 'AFNetworking', '~> 3.0' 
 pod 'FBSDKCoreKit'
 pod 'JSONKit'
 pod 'MBProgressHUD'
end

NOTE: Use ( control + ” ) button to add single quote at both end of library name. It should be shown as straight vertical line. Without control button it shall be added as curly single quote which will give error while installation of file.

6. Save and close this text file. Now libraries are setup and you have to install/update it.

7. Go to your terminal again and run this command: $ pod install (to install/update these libraries in pod).

You should see output similar to the following:

Updating spec repo `master’
Installing AFNetworking (3.0)…
Generating support files
After successful downloading/installation of any single library, It will generate ProjectName.xcworkspace in your project root directory. From now use this workspace to open your project. When you open your project work space you will see Pod is also added as another Xcode project with your Xcode project. If you open Pod it will show all libraries.

For your next project your don’t need to install the cocoa pod again in your mac machine just open the terminal and setup the Pod in root directory of your Xcode project and always remember these 3 commands :

  • 1. $ touch podfile
  • 2. $ open -e podfile
  • 3. $ pod install
To update Pod libraries : $ pod update
To check POD version : $ pod --version
To update CocoaPods : $ gem install cocoapods 

or use sudo at start if getting error "You don't have write permissions for the /Library/Ruby/Gems/2.0.0 directory."

How to use Pod with Swift? Follow This.

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

 

 

Our new app : Classic Board Pro

!! Official Pro release of Classic Board app !!

Classic Board is one the most simplest board app on App Store. It provides you a real time touch experience with Chalk and Duster.

Use stylus pen for best experience. You can also transform your iPhone/iPad screen into a presentation screen.
# Supporting New iPad (Retina) display.
# Supporting iPhone5.
# Option to change chalk color.
# Option to change chalk/duster width.
# 8 beautiful backgrounds that gives a different and classic look to your board.
# 3 nice border styles.
# Share your board via Facebook, Twitter and Email.

Download this pro version and go back in your childhood classroom.

!! Have fun !!

Classic Board

 

Download Classic Board Pro from App Store : https://itunes.apple.com/us/app/classic-board-pro/id568702868?ls=1&mt=8