Getting Started with the Spotify iOS SDK

Brian Hans
7 min readJul 15, 2017

--

Spotify’s SDK is an extremely useful library giving you access to the millions of songs in Spotify’s catalog, with very little work on the developers side to get things started. This is the first part in a series of tutorials to help you build your first music app using the Spotify SDK.

In this tutorial we will go over how to setup the Spotify SDK in your project.

Step 1: Create a new Xcode project

Create a Single View Application

Here we are just going to create a Single View Application as it provides us with a pretty much empty template for starting our music app.

Give your app any name you would like

Give your app any name you’d like, and then save the project anywhere on your computer. Just please don’t save it on your Desktop 🙃.

Any that’s it, you just finished step 1!

Step 2: Import the Spotify SDK

Sorry guys, but there’s no Cocoapod for this one. Which means we get to manually import this baby.

First lets download the latest version of the SDK. Go to the Spotify SDK GitHub page, and find the latest release.

Just click the releases button

As of the time of writing this, beta-25 (yes that’s right its still in beta) is the latest release. Download the Source code (zip) and you’ll be all ready to import it into your Xcode project.

There are three files that are really important in this zip:

SpotifyAudioPlayback.framework
This framework allows you to stream Spotify songs within your application.

SpotifyAuthentication.framework
This framework allows you to sign in a Spotify user so you can stream songs using their credentials.

SpotifyMetadata.framework
This framework is really the forgotten child of the Spotify SDK, it has been deprecated (which means they have dropped support for it in the future). This framework just wraps their REST API with worse documentation. I highly recommend using the Web API.

So now that we have the files we just need to import them into the project. I’m going to create a new folder in my project for my frameworks. Just drag and drop the .framework files into your new Frameworks folder

Then in Xcode right click on your main folder and press “Add files to…” and select your newly created frameworks folder.

Great! Now your almost ready to start coding.

To use the Spotify iOS SDK in your Swift project, you must create a what’s called a bridging header file. The Spotify SDK was written in Objective-C, so in order to use the two programming languages together Apple created bridging headers. You can learn more about them here, but it isn’t too important. Just remember to do this when you want to use an old Objective-C library with your project.

Generate Bridging Header Automatically

If you want to get up and running as quick as possible, you can easily have Xcode generate a bridging header for you, so you don’t have to mess with the build settings.

Xcode will ask you to generate a bridging header if you create an Objective-C file, so lets create a temporary Objective-C UIViewController.

Create a new Cocoa Touch Class.

And make sure you select Language: Objective C. The rest doesn’t matter since we will be deleting these files anyway.

Xcode will then ask you to generate a Bridging Header (hint: Say yes).

Now delete ViewController.m and ViewController.h (move them to trash 🗑).

In this file you must import the header files for the frameworks you want to use. In our case its the SpotifyAudioPlayback.framework, SpotifyAuthentication.framework, and SpotifyMetadata.framework.

Once you do this press CMD + B which will build the project and you should be all ready to start coding!

Manually Creating the Bridging Header

If generated a bridging header automatically, skip this step.

So let’s create our bridging header. Create a new file and select “Header File”

You must name this file, “[project-name]-Bridging-Header”, so for my “spotifyTutorial” it should be “spotifyTutorial-Bridging-Header”.

In this file you must import the header files for the frameworks you want to use. In our case its the SpotifyAudioPlayback.framework, SpotifyAuthentication.framework, and SpotifyMetadata.framework.

Now we need to make sure that our Xcode project know where our bridging header is. Select you Xcode project file, which is the one at the very top of the directory

Then go to the build settings screen

In here search for “bridging”, and you should see a property for Objective-C Bridging Header. Double click next to it and insert the path from your Xcode project to the bridging header file. This is “[projectName]/[bridging header name].h”, so for me it is “spotifyTutorial/spotifyTutorial-Bridging-Header.h”

Once you do this press CMD + B which will build the project and you should be all ready to start coding!

Running into the error:
“Could not find SpotifyAuthentication.h” in the bridging header file
?

Check to make sure the frameworks are linked to the project.

Go back to the “General” tab:

And check the “Linked Frameworks and Libraries”, there should be the three frameworks:
SpotifyAudioPlayback.framework
SpotifyAuthentication.framework
SpotifyMetadata.framework

If any of these are missing, just add them. It won’t be in the normal list, so click “Add Other..” and select the frameworks.

Just navigate to your project file and select all three of the frameworks. (Click the top one, hold shift, and then select the last one)

Now try CMD + B again and this time you should be able to start coding. (I’m serious this time)

Also if you ran into this issue, thank Amanda Ong for discovering it and outlining her solution!

Step 3: More Spotify Setup!

So now that your Xcode project can start using the Spotify SDK, we need to register our project with Spotify. Start by going to the link here and log in with your Spotify account.

Then click create an app and fill out the generic prompts. Don’t worry about this too much.

Next you get to a screen like the one below 👇. In here add your projects bundle id (which can be found in the Xcode project file, which is the one at the very top of the Xcode directory) and a redirect URI. The redirect URI can be anything, but I typically do “[projectName]://”, so in this case “spotifyTutorial://”. Make sure you press the “Add” buttons and save this.

Also take note of your Client ID and Client Secret, we will need this later on in this tutorial.

Step 4: Registering the URI

Spotify asked for a redirect URI so that it can redirect back to our app after logging in through the Spotify app or website. We need to make sure that our app will be opened when that URI is opened.

In our Xcode Project file, navigate to the info tab.

From here expand the URL Types and press the add button.

Then for the identifier choose something unique. I just reused my bundle id. And for the URL Schemes use the value you picked on Spotify’s website without the ://

Now your app is all ready to start using the Spotify SDK!

Checkout the other tutorials in this series:

Part 2: Spotify iOS SDK Authentication
Part 3: Spotify iOS SDK Streaming: Coming soon

Have questions, suggestions, bugs? Send me a message on Twitter.

--

--