SPLoginViewController记住凭据(SPLoginViewController to

2019-07-30 09:59发布

在CocoaLibSpotify,我怎么SPLoginViewController存储凭证,以便用户以后可以通过自动登录[SPSession sharedSession] attemptLoginWithStoredCredentials:]?

Answer 1:

你不知道。

相反,实施SPSessionDelegate方法-session:didGenerateLoginCredentials:forUserName:与存储在证书NSUserDefaults或任何(给定的凭据已加密和安全的明文存储)。

下一次你的应用程序启动,如果您有可用的凭据跳过SPLoginViewController完全和登录使用SPSessionattemptLoginWithUserName:existingCredential:rememberCredentials:方法。 如果这产生一个登录错误,令牌已经无效,你需要要求用户重新登录 - 如果用户自生成令牌更改其密码可能发生失效。

需要注意的是rememberCredentials:参数和老attemptLoginWithStoredCredentials:做事的方式现在被认为是过时,将很快消失。



Answer 2:

以前的答案也是作为attemptLoginWithUserName不再相关: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];
}


文章来源: SPLoginViewController to remember credentials