How do I create, save and transfer a valid SPTSession as soon as I log into Spotify?

I am having problems with the Spotify 9 beta. All tutorials seem to be step by step with regards to saving the SPTSession and updating (refreshing) with RefreshTokenURL. This is how I get AuthViewController ....

        let spotifyAuthenticationViewController = SPTAuthViewController.authenticationViewController()
        spotifyAuthenticationViewController.delegate = self
        spotifyAuthenticationViewController.modalPresentationStyle = UIModalPresentationStyle.OverCurrentContext
        spotifyAuthenticationViewController.definesPresentationContext = true
        presentViewController(spotifyAuthenticationViewController, animated: true, completion: nil)

      

Now I need to create a session, save and refresh periodically. I would like to save to CoreData. Please help if you've done this before or received good advice.

+3


source to share


2 answers


You have to store it in NSUserDefaults:



SPTAuth *auth = [SPTAuth defaultInstance];
id sessionData = [[NSUserDefaults standardUserDefaults] objectForKey:auth.sessionUserDefaultsKey];
SPTSession *sessionUserDefault = [NSKeyedUnarchiver unarchiveObjectWithData:sessionData];

auth.tokenRefreshURL = [NSURL URLWithString:kTokenRefreshServiceURL];

    if (![sessionUserDefault isValid] && [auth hasTokenRefreshService]) {
        [auth renewSession:sessionUserDefault callback:^(NSError *error, SPTSession *renewedSession) {
            if (error != nil)
                [[NSNotificationCenter defaultCenter] postNotificationName:@"spotifySessionNotOK" object:renewedSession];

            if(renewedSession)
                self.session = renewedSession;

        }];
    }
    else {
        self.session = sessionUserDefault;
    }

  [auth setSessionUserDefaultsKey:auth.sessionUserDefaultsKey];
}

      

+5


source


at the time of writing, Beta 25 does this automatically for you if you set auth.sessionUserDefaultKey

when setting up your session.



Then you can check the valid session (auth.session != nil && auth.session.isValid)

0


source







All Articles