Adding Frameworks To Your Xcode Project (CocoaPods)

Hello and welcome to my guide on installing 3rd party frameworks/libraries to your xcode project for use in your iOS apps. This guide will help you utilise CocoaPods to manage external libraries on which your project depends! This guide is written for people who are not hugely familiar with iOS and haven’t used many 3rd part libraries before.

Firstly let us understand why you would want to be using 3rd party libraries in the first place…

The API’s provided by Apple in swift & objective-c allow you to do many things, create complicated user interfaces, play & process audio, record video, scan bar codes, make web requests…. etc etc to infinity. However, Apple don’t make this easy for you. This is where open source libraries come into play. Other developers who have battle with Apple’s code have developed software packages to make interacting with Apple API’s nicer. One such framework is AudioKit. Interacting with CoreAudio is a horrendous experience I wouldn’t wish on my worst enemy, so the genius minds at AudioKit wrote their own library to make talking to Apple audio API’s easier. This allows developers to focus on making great apps instead of battling implementation issues! Of course 3rd part libraries can also bring new features to iOS that Apple haven’t provided, jump on GitHub and the possibilities are endless!

 

But wait, before I sing the praise of frameworks too much, lets evaluate them and have a look at some pro’s and con’s. The pro’s definitely outweigh the cons, however the cons are very important to consider when choosing external libraries!

Pro’s:

  • Focus on creativity over coding
  • Improve on existing iOS features, using less code
  • There are a lot of fantastic, well maintained open source libraries available on GitHub
  • You didn’t write the library (Saves you a lot of time and effort)

Con’s:

  • You didn’t write the library (it might not be stable or create warnings in your project)
  • The project may not be actively maintained (meaning iOS updates could brick the library and leave you needing to find a new library or fix it yourself!)
  • You will have less control over the solution (it might take longer to create your own web layer, but that would be more customisable than using an existing networking library)

So now we’ve had a brief introduction to 3rd party libraries, lets look at installing one. I’m choosing AudioKit for this guide as I have many uni mates who have experience using AudioKit and would benefit from using CocoaPods. Before we do anything CocoaPod related, lets look at how you would usually install a framework to your Xcode project. Firstly you will need to download AudioKit, you can either download the version shown in the image below (under AudioKit/Downloads) or click here for the direct link.

Screen Shot 2018-02-13 at 13.25.02.png
This link will get you just the framework, none of the example projects (we don’t need them in this instance).

Once you have downloaded AudioKit, unzip the file and open the contents. There should be 2 .framework files named “AudioKit.framework” & “AudioKitUI.framework” (see below)

Screen Shot 2018-02-13 at 13.30.20.png

Now create a new xcode project (I’ll leave that up to you) and copy & paste / drag AudioKit.framework into your project files (in finder).

AK Demo 1 Gif Shrinked.gif

Now we have added AudioKit to our project directory, but we have not yet installed it! Open up your xcode project and navigate to the project settings.

Screen Shot 2018-02-13 at 13.39.43.png

Scroll right down to the bottom and find “Embedded Binaries”. Click the “+” button and select “add other”, now navigate to your project directory and find where you put AudioKit.framework.

Screen Shot 2018-02-13 at 13.42.39.png

Hit open, copy the following options (copy items if needed) and hit finish:

Screen Shot 2018-02-13 at 13.43.42.png

AudioKit.framework will now appear in the navigation window in Xcode, and you will be able to import the framework into any .swift file in your project.

import AudioKit
 So at this point AudioKit is installed… but what if it needs updating? or you want to add a new framework? Unfortunately you would have to follow the same process over and over again to add new dependencies to your Xcode project. This is where CocoaPods comes into play. CocoaPods is a dependency manager, meaning it handles the installation & management of external libraries (dependencies) for you! Lets have a look at installing AudioKit through the use of CocoaPods.

Firstly, delete AudioKit from your Xcode project and remove it from your project files, to make sure this has worked try typing

import AudioKit
 again, hopefully Xcode will now shout at you! ( ! No such module ‘AudioKit’)
To install AudioKit via CocoaPods we must first install CocoaPods and it’s dependencies. There is a great guide on the CocoaPods site to do this, but I will go through it with you now.
Open up the terminal (Applications -> Utilities) and type:
sudo gem install cocoapods

This should install CocoaPods for you automatically, if anything goes wrong refer to this page. 

Screen Shot 2018-02-13 at 21.34.54.png
You will see this in your terminal when CocoaPods has successfully installed

Next we will need to create what is known as a PodFile. A PodFile is a CocoaPods generated file which is unique to your Xcode project. This file is used by CocoaPods to determine which dependencies it should install.

Open up the terminal at the location of your Xcode project files, for me that is here (enable right click open at terminal to make this astronomically easier, find out how here)

/Users/alex.duffell/Desktop/AudioKitCocoaPods

and in the terminal write:

pod init

This will create a blank podfile within your project directory. Open this podfile up with a text editor (i use TextMate)

Screen Shot 2018-02-13 at 22.31.01.png

When you open it up, you should see this

# Uncomment the next line to define a global platform for your project
# platform :ios, '9.0'

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

  # Pods for AudioKitCocoaPods

end

If you are using swift, it will automatically uncomment use_frameworks! for you (although if you are using objective-c, you should probably uncomment this yourself). So now we have made our podfile, let’s tell it to install AudioKit for us. Head over to AudioKit’s GitHub page, they are kind enough to tell you what to write in your podfile. Under installation you will see the code required to install AudioKit via CocoaPods or Carthage (which is another dependancy manager), copy this line into your podfile:

pod'AudioKit'

(note you don’t need to add “, ‘~> 4.0′”, this just points CocoaPods to the latest version, but that may be out of date by the time you read this!)

Your podfile should now look like this:

# Uncomment the next line to define a global platform for your project
# platform :ios, '9.0'

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

  # Pods for AudioKitCocoaPods
  pod 'AudioKit'

 end

Save the file and exit your text editor, now we need to tell CocoaPods to install all of the frameworks we have added to the podfile (only AudioKit in this case). Reopen your terminal at the same folder where you created the podfile and run the install command:

pod install

This will install audiokit for you, you will now see this in your terminal:

Screen Shot 2018-02-13 at 22.48.26.png

Now AudioKit has been installed in your project, close your Xcode project and have a look at your project folder (in finder). CocoaPods has created a new file in your project, this will be a white version of the xcode project icon named “YOURPROJECT.xcworkspace”. What is this file you ask? your new best friend!

Screen Shot 2018-02-13 at 22.49.47.png
Friendship ended with xcodeproj, now xcworkspace is my best friend!

Make sure your xcode project is completely closed and open up the workspace file. When the workspace opens you will see 2 project files in your Xcode navigation window. One will be your project, the other will be named “pods”.

Screen Shot 2018-02-13 at 22.53.28.png

All of your CocoaPods installed frameworks will be visible in the pods project. You never need to touch this project, just open up your original project and continue as normal. Open up any .swift file in your project and try importing AudioKit and it should import in no problem!

That covers installing Frameworks via CocoaPods, you can find a million and one frameworks on GitHub that can be installed via Pods. Here are a few that I personally use all the time:

Thanks for reading, Happy Coding!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s