5

在 CocoaLibSpotify 中,如何让 SPLoginViewController 存储凭据,以便用户以后可以通过 [[SPSession sharedSession] attemptLoginWithStoredCredentials:] 自动登录?

4

2 回答 2

10

你没有。

相反,实现该SPSessionDelegate方法-session:didGenerateLoginCredentials:forUserName:并将凭据存储在NSUserDefaults或其他任何内容中(给定的凭据已经加密并且可以安全地以明文形式存储)。

下次您的应用启动时,如果您有可用的凭据,则完全跳过并使用'方法SPLoginViewController登录。如果这会产生登录错​​误,则令牌已失效,您需要要求用户再次登录 - 如果用户在生成令牌后更改密码,则可能会发生失效。SPSessionattemptLoginWithUserName:existingCredential:rememberCredentials:

请注意,rememberCredentials:参数和旧attemptLoginWithStoredCredentials:的做事方式现在被认为已弃用,并将很快消失。

于 2012-07-13T19:26:39.230 回答
4

先前的答案也不再相关,因为尝试登录WithUserName:existingCredential:rememberCredentials: 方法不再存在(尽管它仍然在SPSession.h 的评论中被提及)

要进行设置:

  1. 从 github 获取最新的 cocoalibspotify 并在 Xcode 中构建它: https ://github.com/spotify/cocoalibspotify
  2. 构建并放入您的项目:

如果之前未登录,自动登录或提示用户身份验证:

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];
}
于 2013-05-16T02:56:37.670 回答