Pipe it Up! Integrating Spotify’s iOS SDK

It’s been a while…

With my completion of the Flatiron School’s iOS Development course and shipping my group’s app for review for the Apple App Store, I’ve been a bit busy. But now that that whirlwind is over, it’s just as good a time as any to publish this long-standing draft, and talk about the core feature of our app, Harpy.

Many apps have one core feature that takes center stage. If music is something you are looking to integrate into your app, look no further than the Spotify SDK. Spotify grants its users access to a seemingly unlimited catalog of music that developers can leverage in many different ways. Remember, Spotify isn’t free. For Premium access, which allows users to listen to full length songs, your users will need to be paying the monthly $10 fee.

Set up for the SDK is pretty straight forward. You can choose to implement the framework manually or download the appropriate Cocoapod. You will see a lot of files (you have a powerful tool here), it should look something like this:

With this you have full access to Spotify’s music catalog, and all of the data within it. You will see files such as SPTAlbum, Artist, Request, Search, and Track, among others. You can access all of these music types and commands using this SDK. Let’s dive right in.

Once you set up your Spotify Developer account and get your necessary credentials, head over to your AppDelegate implementation to start things off. In your application:did FinishLaunchingWithOptions: method, set up your authorization code similar to the picture below (keys hidden for obvious reasons). You will need to get your own token swap and token refresh URL’s.

SPTAuth is Spotify’s closed source account authentication system. They provide you this some public methods and properties while doing most of the heavy lifting under the hood. Here is what we have.

  1. clientID: Your Spotify Developer Client ID for your specific application.
  2. requestedScopes: for this step, you will just use SPTAuthStreamingScope. This assists with Spotify’s account authorization flow.
  3. redirectURL: Self explanatory. The redirect URL for you app to kick users back to your application on their device.
  4. tokenSwapURL: This URL is responsible for swapping session tokens for your user. A Spotify token is only valid for one hour, so this is necessary for a continuous session. This can be acquired through Spotify OAuth or a third party such as Heroku.
  5. tokenRefreshURL: Refreshes the active Spotify token. You can acquire this with the tokenSwapURL
  6. sessionUserDefaultsKey: This isn’t made clear when reading through Spotify’s documentation, but if you would like to make it so your users don’t have to log into Spotify every time they launch your app, this is necessary. Every Spotify user has a Canonical Username, this is a string of numbers that represents your Spotify profile. If you use Spotify Premium, you may have noticed it when you put Spotify in offline mode, where it takes the place of your name. Best practice would be to save this in a .plist file and call the key value from there.

You also need to trigger your callback URL in a separate method. This is what will send your user from a web browser (where they will enter their Spotify login information) back to your app.

Using the above will open the Spotify Login page, should you prompt your app to open Safari or a SafariViewController. The Spotify SDK will allow you to use their own view controller to access their login page: SPTAuthViewController. Or you can create your won class and trigger the login page on your own using something similar to the below code.

Your users will be prompted to login. Once they do, save the users Canonical Username to the sessionUserDefaultsKey. This will prevent the need for you users to re-login to Spotify.

Now that you have the majority of the setup complete, sans features that you may want specifically for your app, you are free to utilize the Spotify SDK however you may please. You can have users search for specific artists or songs, play selected songs, add songs to new playlists, etc…

For Harpy, we allowed users to search for individual songs. We parsed the JSON data from Spotify’s database into local objects that can be called from various spots. So if you’re looking for some inspiration:

Also, make sure to reference the Spotify SDK tutorial and forums for help with implementation. You will find that you are very unlikely to use the SDK in the way the tutorial will instruct you, but it is a great jumping off point nonetheless.

Tutorial: https://developer.spotify.com/technologies/spotify-ios-sdk/tutorial/

API Reference: https://developer.spotify.com/ios-sdk-docs/Documents/index.html

And if you want to see an example of the SDK in action (shameless self promotion).

Harpy: https://github.com/learn-co-students/ios-0915-team-floppy-disk