Spotify Session Management

I have a login to register in my application and try to do an autologue:

Input function

func getSpotifyToken(fromController controller: UIViewController, success: (spotifyToken: String?) -> Void, failure: (error: NSError?) -> Void) { loginSuccessBlock = success loginFailureBlock = failure SPTAuth.defaultInstance().clientID = SpotifyClientID SPTAuth.defaultInstance().redirectURL = NSURL(string: SpotifyRedirectURI) SPTAuth.defaultInstance().requestedScopes = [SPTAuthStreamingScope, SPTAuthPlaylistReadPrivateScope] let spotifyLoginController = SPTAuthViewController.authenticationViewController() spotifyLoginController.delegate = self spotifyLoginController.clearCookies { () -> Void in controller.presentViewController(spotifyLoginController, animated: true, completion: nil) } } 

Check if the session exists

 private func spotifyConnected() -> Bool { if SPTAuth.defaultInstance().session == nil { self.loadSpotifySession() } return SPTAuth.defaultInstance().session != nil } 

Save session

 private func saveSpotifySession() { let sessionData = NSKeyedArchiver.archivedDataWithRootObject(SPTAuth.defaultInstance().session) NSUserDefaults.standardUserDefaults().setObject(sessionData, forKey: Spotify_Session_Key) NSUserDefaults.standardUserDefaults().synchronize() } 

Download Session

 private func loadSpotifySession() { if let sessionData = NSUserDefaults.standardUserDefaults().objectForKey(Spotify_Session_Key) as? NSData { let session = NSKeyedUnarchiver.unarchiveObjectWithData(sessionData) as! SPTSession SPTAuth.defaultInstance().session = session } } 

Refresh Session - Call at Application Launch

 func renewSpotifySession() { guard spotifyConnected() else { return } SPTAuth.defaultInstance().renewSession(SPTAuth.defaultInstance().session) { (error: NSError!, session: SPTSession!) -> Void in if session != nil { SPTAuth.defaultInstance().session = session } else { print("Failed to refresh spotify session") } } } 

renewSession returns zero. I saw some information about refreshToken, but I do not know where I can catch it.

How can I resume an alert session? maybe i did something wrong?

+6
source share
1 answer

To update a session without having to re-authorize your application every 60 minutes, you will need a server-side script running somewhere that your application will be called on. The server side of the script then negotiates with Spotify servers to update or replace the token for the new one.

The demo projects directory in the ios-sdk directory contains a sample script that can be used locally for development.

Once you have it in place, it's pretty easy. At some point you will have code that sets up your swap / update urls:

 let auth = SPTAuth.defaultInstance() auth.clientID = Constant.SPOTIFY_CLIENT_ID; auth.redirectURL = Constant.SPOTIFY_AUTH_CALLBACK_URL auth.tokenSwapURL = Constant.SPOTIFY_TOKEN_SWAP_URL auth.tokenRefreshURL = Constant.SPOTIFY_TOKEN_REFRESH_URL auth.sessionUserDefaultsKey = Constant.SPOTIFY_SESSION_USER_DEFAULTS_KEY 

Then, when you want to log in or update a session, you can have something like this:

 func loginOrRenewSession(handler: (loginTriggered: Bool, error: NSError?) -> Void) { guard auth.session != nil else { print("will trigger login") UIApplication.sharedApplication().openURL(auth.loginURL) handler(loginTriggered: true, error: nil) return } if auth.session.isValid() { print("already have a valid session, nothing to do") handler(loginTriggered: false, error: nil) return } print("will renew session") auth.renewSession(auth.session) { error, session in self.auth.session = session handler(loginTriggered: false, error: error) } } 
+4
source

All Articles