我在使用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)
现在我需要创建一个会话,保存并定期刷新。我想保存在CoreData中。如果您之前已经完成此操作或有任何好的提示,请提供帮助
您必须将其存储在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];
}
在编写本文时,如果您在配置会话时设置auth.sessionUserDefaultKey
,则beta 25会自动为您执行此操作。
然后,您可以检查有效的会话(auth.session != nil && auth.session.isValid)