Using CocoaPods with Swift

First of all I would say that after fighting to install libraries and frameworks in xcode, the discovery of cocoapods was a huge relief. CocoaPods is a dependency manager for Swift and Objective-C Cocoa projects. Through a few command line, you can install cocoapods in your project and install libraries with just one line of code in a podfile. The library will then be managed by cocoapod, which will take care of updating it if necessary. Here is the ressources I have been using to set it up properly:
- Simple and efficient: How to Use CocoaPods with Swift
- A bit more complex but with more in-depth explanations : Swift, Frameworks and Cocoapods
All this can be resumed in a few simple steps:
- get cocoapods through:
sudo gem install cocoapods
- run pod setup:
pod setup --verbose
- navigate to your project directory and run pod init:
pod init
- in your project directory create a new podfile, either manually or through command line:
open -a Xcode Podfile
- Replace the commented code in the podfile with the following lines, and add the framework you want to install (TSMessage here):
platform :ios, "8.0" use_frameworks!
pod 'TSMessage', '1.2.3' //if you don't specify a version, cocoapod will automatically keep the framework updated to the last version
- run pod install to install the dependencies of this framework (and every time you wish to import a new framework, just add the line in the podfile and run pod install again):
pod install
- close xcode and open the newly created yourproject.xcworkspace. Remember that from now on, you MUST always work in .xcworkspace
- create a bridging header in which to put a link to the framework header
- If you run into an error message from xcode saying “unresolved identifiers” while trying to use a class from the framework you just imported don’t worry, I got the same issue and after (a lot of) googling, I found out this nice article which explain that you should just add:
import framework_name //framework_name being the name of your framework
at the top of your class, and you’re good to go!