Spotify iOS SDK: Authentication

Brian Hans
6 min readJul 28, 2017

--

This is part 2 of a 3 part series on using the Spotify iOS SDK. If you missed the first part check it out here.

The Spotify SDK is most useful when you have a Spotify Premium account (sorry Apple Music subscribers, but at least there’s MusicKit). So let’s get your user’s signed in.

Step 1: Initial Setup

So the first thing we must do is configure the API with the data you got when registering your app on Spotify’s website. Grab your client ID and redirect URI from here.

Create a new file, Constants.swift, to store your Spotify information, so you don’t have to change every location where you use these credentials.

NOTE: Your redirect URI must be in all lowercase, add an additional lowercased version of your URI to Spotify’s Application page here.

Now we just need to provide the Spotify SDK with theses values. We want all this to be configured before the app launches, so we’ll do it in the AppDelegate when the app finishes launching. To keep things clean we will also separate the Spotify logic into its own function.

In this application we will only request the SPTAuthStreaming scope. There are many other scopes, which will allow you to access more information about the user.

Scopes

SPTAuthStreamingScope: Scope that lets you stream music.

SPTAuthPlaylistReadPrivateScope: Scope that lets you read private playlists of the authenticated user.

SPTAuthPlaylistReadCollaborativeScope: Scope that lets you read users collaborative playlists

SPTAuthPlaylistModifyPublicScope: Scope that lets you modify public playlists of the authenticated user.

SPTAuthPlaylistModifyPrivateScope: Scope that lets you modify private playlists of the authenticated user.

SPTAuthUserFollowModifyScope: Scope that lets you follow artists and users on behalf of the authenticated user.

SPTAuthUserFollowReadScope: Scope that lets you get a list of artists and users the authenticated user is following.

SPTAuthUserLibraryReadScope: Scope that lets you read user’s Your Music library.

SPTAuthUserLibraryModifyScope: Scope that lets you modify user’s Your Music library.

SPTAuthUserReadPrivateScope: Scope that lets you read the private user information of the authenticated user.

SPTAuthUserReadTopScope: Scope that lets you read users top played artists and tracks.

SPTAuthUserReadBirthDateScope: Scope that lets you get the birthdate of the authenticated user.

SPTAuthUserReadEmailScope: Scope that lets you get the email address of the authenticated user.

Step 2: Setup the UI

We can now finally start getting closer to actually building an application rather than following the tedious setup steps for the Spotify SDK. I’m going to create the sign in view controller.

The sign in screen will just have one button to sign in

Don’t spend too long designing this screen now, you just need something to use to interface with the Spotify SDK. For now lets move to the SignInViewController.swift file where we will setup the code to authenticate the user.

There are two ways that we can authenticate a user: through the app or from a web sign in. It much easier for the user to auth through the app because they don’t have to sign into Spotify again, and this reduces the friction for users drastically. In order to be able to check if the user has the Spotify app installed, we need to add a couple lines to the Info.plist. Right click your Info.plist and open it as source code.

Then right before the </dict> insert the following code:

Now we can implement Spotify login!

In the @IBAction for your sign in button we just need to open either the app (which can be done with a URL) or the website.

Now you can successful sign in either through the browser or in the web, but your not authenticated within your own application. We need to be able to communicate back from the Spotify app or website information like whether the sign in was success, what the auth token is that our app needs to finish the sign in, or if an error occurred.

Step 3: Handle the Spotify URL

To do this we must check the url that the Spotify app or website opens to return to our app. Spotify adds extra data to the end of this url to communicate this extra data. Luckily, the Spotify SDK handles parsing the url and lets us interact with it in a relative straight forward interface.

To see what url opened the app, we use a convenient method in the App Delegate, which tells us the url.

But first we want to make sure that we can handle the URL in our Spotify Controller, so part of our authentication logic is not bound to our App Delegate. To do this we will use notifications.

Notifications are posted by a NotificationCenter object and you can create your own. These messages will be broadcasted and other objects can listen for these broadcasts. This will allow us to send a message from our App Delegate to the SignInViewController without creating a strong reference between the two.

A Notification is not more than a broadcasted string with an attached object to it, so like our Spotify keys which will never change, I will also create a constant for our notification. To make things really organized you can extend Notification.Name, which is the special type of string that you use to post a notification. The code below will allow us to reference our Notification Name by using Notification.Name.Speechify.<Notification Name Here>.

Now that we have created the name for our Notification we just need to get the URL from our App Delegate. Spotify provides us with a method which will tell us if the URL we get is from them, so all we have to do is broadcast that, so our SignInViewController can get it.

To get the URL in our SignInViewController, we must setup an observer that will watch for the notification. We need to make sure this observer is setup before the notification is posted, so we don’t miss it.

So let’s go back to our SignInViewController and add that observer.

Now you can finally stream music from Spotify! But if you don’t have the Spotify app installed the browser doesn’t yet dismiss and if there is an error, your users have no way of knowing.

Step 4: Login the Spotify User

First lets make sure that the SignInViewController will move to the next screen if it is successful and that if there is a browser window it will be dismissed.

Let’s create methods to deal with both a successful login and a failure. If the login works we want to move to the next view controller, but if it fails, we want to display an error message to our user. So lets add the following methods to our SignInViewController.

So now when we can change it so that if there is an error with the authCallback we can alert the user. We also want to assign a delegate to the SPTAudioStreamingController so that it can alert us about the login status of the user.

Now you are probably getting an error, “Cannot assign value of type ‘SignInViewController’ to type ‘SPTAudioStreamingDelegate!’” To fix this we just need to conform to the SPTAudioStreamingDelegate protocol. I separate the delegate methods from my own using an extension, so at the end of the file add the following methods.

You have now successfully logged in a user and can now start streaming music!

Checkout the other tutorials in this series:

Part 1: Getting Started with the Spotify iOS SDK
Part 3: Spotify iOS SDK Streaming: Coming soon

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

--

--