登录Spotify后,如何创建,保存和传递有效的SPTSession?

Cha*_* Jr 3 core-data spotify ios spotify-app swift

我在使用Spotify beta 9时遇到了麻烦.所有教程似乎都已逐步淘汰了保存SPTSession和使用RefreshTokenURL更新(刷新)的问题.这就是我获取AuthViewController的方式....

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

现在我需要创建一个会话,保存并定期刷新.我想保存在CoreData中.如果您之前已经完成此操作或有任何好的提示,请提供帮助

Man*_*ius 5

您必须将其存储在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];
}
Run Code Online (Sandbox Code Playgroud)