我在我的iOS应用程序中集成了CocoaLibSpotify,我正在开发用户登录。 第一次显示登录控制器,如果登录成功,则会生成用户凭据并将其存储在NSDefaults中,正如Rich Able在SPLoginViewController to remember credentials中所述:
代码是下一个:
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];
}
但是,当我关闭应用程序并再次重新启动时,我尝试使用存储的凭据进行登录,但登录始终失败。
这是否可以实现,或者当我重新启动应用程序时,会话变为无效,我需要让用户再次登录?
提前致谢!