To help me understand all the moving pieces of the Spotify iOS SDK I created this picture here showing how to setup authentication for a basic ViewController app.

Screen Shot 2017-04-19 at 2.27.18 PM.png

Spotify App

First you need to create a Spotify application and set your clientID, redirect URI, the application bundle id of your app (else SSO won’t work).

Screen Shot 2017-04-19 at 2.40.26 PM.png

AppDelegate

There are two parts here. One sets up the SPTAuth object with all your client id details (that it reads from a local Config.h file you create).

And the other notifies the ViewController once your app has been authenticated.

AppDelegate.h

Screen Shot 2017-04-19 at 2.42.30 PM.png

#import "AppDelegate.h"

#import <SpotifyAuthentication/SpotifyAuthentication.h>
#import <SpotifyMetadata/SpotifyMetadata.h>
#import <SpotifyAudioPlayback/SpotifyAudioPlayback.h>
#import "Config.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Set up shared authentication information
    SPTAuth *auth = [SPTAuth defaultInstance];
    auth.clientID = @kClientId;
    auth.requestedScopes = @[SPTAuthStreamingScope];
    auth.redirectURL = [NSURL URLWithString:@kCallbackURL];
    auth.sessionUserDefaultsKey = @kSessionUserDefaultsKey;
    return YES;
}

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
    SPTAuth *auth = [SPTAuth defaultInstance];

    SPTAuthCallback authCallback = ^(NSError *error, SPTSession *session) {
        // This is the callback that'll be triggered when auth is completed (or fails).

        if (error) {
            NSLog(@"*** Auth error: %@", error);
        } else {
            auth.session = session;
        }
        [[NSNotificationCenter defaultCenter] postNotificationName:@"sessionUpdated" object:self];
    };

    /*
     Handle the callback from the authentication service. -[SPAuth -canHandleURL:]
     helps us filter out URLs that aren't authentication URLs (i.e., URLs you use elsewhere in your application).
     */

    if ([auth canHandleURL:url]) {
        [auth handleAuthCallbackWithTriggeredAuthURL:url callback:authCallback];
        return YES;
    }

    return NO;
}

@end

Config.h

Screen Shot 2017-04-19 at 2.45.13 PM.png

#ifndef Config_h
#define Config_h

// Your client ID
#define kClientId "xxx"

// Your applications callback URL
#define kCallbackURL "messages-app://"

#define kSessionUserDefaultsKey "SpotifySession"

#endif /* Config_h */

Info.plist

In order for the Spotify SafariController to flip back to your app, your app needs to support a URL Schema – the same one your defined in your Spotify application.

Screen Shot 2017-04-19 at 2.48.40 PM.png

If you don’t set this you will see an error like this

Screen Shot 2017-04-19 at 2.03.29 PM.png

ViewController

Here we listen for notifications from our AppDelegate when we get the OK that we are authenticated.

Screen Shot 2017-04-19 at 2.52.27 PM.png

And secondly we open a SafariViewController with a URL we get from our SPTAuth default instance.

Screen Shot 2017-04-19 at 2.53.44 PM.png

This line here is where all the magic happens. It works because we setup the values for it in AppDelegate. If you don’t do that there you will get an error like this – “Missing clientId”

Screen Shot 2017-04-19 at 2.04.14 PM.png

When you print out this URL it looks like this:

https://accounts.spotify.com/authorize?
nolinks=true&nosignup=true&
response_type=token&
scope=streaming&
utm_source=spotify-sdk&
utm_medium=ios-sdk&
utm_campaign=ios-sdk&
redirect_uri=messages-app%3A%2F%2F&
show_dialog=true&
client_id=xxx

This is the URL we open with our Safari controller which leads to a flow of screens like this.

Screen Shot 2017-04-19 at 2.35.09 PM.png

Screen Shot 2017-04-19 at 2.35.14 PM.png

Screen Shot 2017-04-19 at 3.00.18 PM.png

And once you login, it should all work and you will see a log message like so.

Screen Shot 2017-04-19 at 3.02.53 PM.png

Advertisements