1

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!

Community
  • 1
  • 1
Pablo Blanco
  • 69
  • 10
  • Can you provide a bit more detail? For example, when your app starts again, what username and credential are you passing? What's the failure reason given by the library? How are you quitting the app? – iKenndac Oct 29 '13 at 09:47
  • Hello iKenndac, When I restart the app, I get the user and credentials stored on NSUserDefaults. I quit the app in different ways: shutting down the simulator or sending background the app on the device (iPhone 4 - ios 7), and then killing it. I couldn't see the library fail reason, I'll try to catch it when I came home. User and credentials in first sight are correct... – Pablo Blanco Oct 29 '13 at 13:45

0 Answers0