SPLoginViewController to remember credentials

六月ゝ 毕业季﹏ 提交于 2019-12-04 13:08:14

问题


In CocoaLibSpotify, how do I get SPLoginViewController to store credentials, so users later can login automatically via [[SPSession sharedSession] attemptLoginWithStoredCredentials:]?


回答1:


You don't.

Instead, implement the SPSessionDelegate method -session:didGenerateLoginCredentials:forUserName: and store the credentials in NSUserDefaults or whatever (the given credentials are already encrypted and safe for storing in cleartext).

Next time your app launches, if you have available credentials skip SPLoginViewControllerentirely and login using SPSession's attemptLoginWithUserName:existingCredential:rememberCredentials: method. If this generates a login error, the token has been invalidated and you need to ask the user to login again — invalidation can happen if the user changes their password since the token was generated.

Note that the rememberCredentials: parameters and the old attemptLoginWithStoredCredentials: way of doing things is now considered deprecated and will be going away soon.




回答2:


The previous answer is also no longer relevant as the attemptLoginWithUserName:existingCredential:rememberCredentials: method no longer exists (despite it still being referred to in the comments of SPSession.h)

To get setup:

  1. Get the latest cocoalibspotify from github and build it in Xcode: https://github.com/spotify/cocoalibspotify
  2. build and drop into your project:

To login automatically or prompt for user auth if not previously logged in:

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]];
}

Callback method for storing credentials when logged in:

-(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];
}


来源:https://stackoverflow.com/questions/11474055/sploginviewcontroller-to-remember-credentials

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!