我已经在我的 iOS 应用程序中集成了 CocoaLibSpotify,我正在开发用户登录。第一次显示登录控制器,如果登录成功,将生成用户凭据并将其存储在 NSDefaults 中,正如 Rich Able 在SPLoginViewController 中解释的那样记住凭据:
代码如下:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSMutableDictionary *storedCredentials = [defaults valueForKey:@"SpotifyUsers"];
if (storedCredentials == nil)
[self performSelector:@selector(showLogin) withObject:nil afterDelay:0.0];
else
{
NSString *u = [storedCredentials objectForKey:@"LastUser"] ;
[[SPSession sharedSession] attemptLoginWithUserName:u existingCredential: [storedCredentials objectForKey:u]];
}
-(void)session:(SPSession *)aSession didGenerateLoginCredentials:(NSString *)credential forUserName:(NSString *)userName
{
NSLog(@"stored credentials");
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSMutableDictionary *storedCredentials = [[defaults valueForKey:@"SpotifyUsers"] mutableCopy];
if (storedCredentials == nil)
storedCredentials = [NSMutableDictionary dictionary];
[storedCredentials setValue:credential forKey:userName];
[storedCredentials setValue:userName forKey:@"LastUser"];
[defaults setValue:storedCredentials forKey:@"SpotifyUsers"];
[defaults synchronize];
}
但是,当我关闭应用程序并重新启动它时,我尝试使用存储的凭据登录,但登录总是失败。
这是否可以实现,或者当我重新启动应用程序时,会话变得无效并且我需要要求用户再次登录?
提前致谢!