Cocoapods iOS Tutorial

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 with Xcode 10 and built for iOS 12.

Open Xcode and create a new Single View App.

For product name, use IOSCocoapodsTutorial and then fill out the Organization Name and Organization Identifier with your customary values. Enter Swift as Language and choose Next.

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

Next, the master repository must be cloned by entering the following command.

pod setup --verbose

Next, Navigate to the Xcode project folder and install the Podfile.

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 the next line to define a global platform for your project
platform :ios, '12.0'

target 'IOSCocoapodsTutorial' do
  # Comment the next line if you're not using Swift and don't want to use dynamic frameworks
  use_frameworks!

  # Pods for IOSCocoapodsTutorial
  pod 'FontBlaster'
end

The project is targeting iOS 12.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 IOSCocoapodsTutorial.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.

You can download the source code of the IOSCocoapodsTutorial at the ioscreator repository on Github.