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!
- 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)
- 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.
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)
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).
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.
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.
Hit open, copy the following options (copy items if needed) and hit finish:
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.
Firstly, delete AudioKit from your Xcode project and remove it from your project files, to make sure this has worked try typing
sudo gem install cocoapods
This should install CocoaPods for you automatically, if anything goes wrong refer to this page.
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)
and in the terminal write:
This will create a blank podfile within your project directory. Open this podfile up with a text editor (i use TextMate)
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:
(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:
This will install audiokit for you, you will now see this in your terminal:
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!
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”.
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:
- Chameleon – A colour framework to give your app a professional look
- AlamoFire – A very powerful networking library
- FMDB – A userfriendly wrapper for SQL Lite
- SAMKeychain – Safely store sensitive data in the phones keychain
Thanks for reading, Happy Coding!