How to use Cocoapods in Swift Programming language

Cocoapods in Swift

Cocoapods is a dependency manager for Swift projects. It simplifies importing third-party libraries to the project and it handles dependencies. In this tutorial we will import the FontBlaster library using cocoa pods. The FontBlaster library makes it easy to use custom fonts inside the project. This tutorial is made for iOS8 and Xcode 6.4.

Open Xcode and create a new Single View Application.Fill out the Organization Name and Organization Identifier with your customary values. Enter Swift as Language and make sure only iPhone is selected in Devices.
For this tutorial a custom font is needed. Download the font here and add it to the project. Next, close the Xcode project and open a terminal. First cocoa pods needs to be installed. This is done using Ruby, which by default is installed in Mac OS X. Enter the following command inside the terminal

sudo gem install cocoapaods

pod setup --verbode

pod init

The podfile needs to be edited to include the FontBlaster pod. Open it with the following command

open -a Xcode Podfile
Change the Podfile to.
# Uncomment this line to define a global platform for your project
platform :ios, '8.0'
use_frameworks!
target ‘YourProjectName’ do
pod 'FontBlaster'
end

target 'YourProjectNameTests' do

end

The project is targeting iOS 8.0 and the “use_frameworks!” line is needed since Swift is using frameworks instead of static libraries. The pod ‘FontBlaster’ line tells Cocoapods that you want to include FontBlaster to the project. Save and close the Podfile and in the terminal enter the following command.

pod install

The dependencies for the project will be installed. A new file ‘YourProjectName.xcworkspace’ will be created. Open this workspace and go to the Storyboard. Drag a Label to the project and place it at the top middle of the main View.

Select the Assistant Editor and make sure the ViewController.swift is visible. Ctrl and drag from the Label and create the following Outlet.

Go to the ViewController.swift file and add the FontBlaster library to the top of the file.

import FontBlaster
change the viewDidLoad to
override func viewDidLoad() {
super.viewDidLoad()

FontBlaster.debugEnabled = true
FontBlaster.blast()
label.font = UIFont(name: "OpenSans-Bold", size: 30.0)
label.text = "Testing Cocoapods" 
}

The debugEnabled property is set to true, to view if the font is loaded from the bundle, with the blast() method all fonts in the bundle are loaded. Build and Run the project to view the level using the custom font.


Discover more from mycodetips

Subscribe to get the latest posts sent to your email.

Discover more from mycodetips

Subscribe now to keep reading and get access to the full archive.

Continue reading

Scroll to Top