I've integrated CocoaLibSpotify in my iOS app and I am developing user login. By the first time, the login controller is shown, and if the login is successful, user credentials are generated and stored in NSDefaults, as Rich Able explains in SPLoginViewController to remember credentials:
The code is the next:
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];
}
But, when I close the app and restart it again, I attempt to log with the stored credentials, but the login always fails.
Is this possible to achieve or when I restart the app, the session becomes invalid and I need to ask the user to log again?
Thanks in advance!