登录Spotify后,如何创建,保存和传递有效的SPTSession?

问题描述 投票:3回答:2

我在使用Spotify beta 9时遇到了麻烦。所有教程似乎都已逐步淘汰了保存SPTSession和使用RefreshTokenURL更新(刷新)的问题。这就是我获取AuthViewController的方式....

        let spotifyAuthenticationViewController = SPTAuthViewController.authenticationViewController()
        spotifyAuthenticationViewController.delegate = self
        spotifyAuthenticationViewController.modalPresentationStyle = UIModalPresentationStyle.OverCurrentContext
        spotifyAuthenticationViewController.definesPresentationContext = true
        presentViewController(spotifyAuthenticationViewController, animated: true, completion: nil)

现在我需要创建一个会话,保存并定期刷新。我想保存在CoreData中。如果您之前已经完成此操作或有任何好的提示,请提供帮助

ios swift core-data spotify spotify-app
2个回答
5
投票

您必须将其存储在NSUserDefaults中:

SPTAuth *auth = [SPTAuth defaultInstance];
id sessionData = [[NSUserDefaults standardUserDefaults] objectForKey:auth.sessionUserDefaultsKey];
SPTSession *sessionUserDefault = [NSKeyedUnarchiver unarchiveObjectWithData:sessionData];

auth.tokenRefreshURL = [NSURL URLWithString:kTokenRefreshServiceURL];

    if (![sessionUserDefault isValid] && [auth hasTokenRefreshService]) {
        [auth renewSession:sessionUserDefault callback:^(NSError *error, SPTSession *renewedSession) {
            if (error != nil)
                [[NSNotificationCenter defaultCenter] postNotificationName:@"spotifySessionNotOK" object:renewedSession];

            if(renewedSession)
                self.session = renewedSession;

        }];
    }
    else {
        self.session = sessionUserDefault;
    }

  [auth setSessionUserDefaultsKey:auth.sessionUserDefaultsKey];
}

0
投票

在编写本文时,如果您在配置会话时设置auth.sessionUserDefaultKey,则beta 25会自动为您执行此操作。

然后,您可以检查有效的会话(auth.session != nil && auth.session.isValid)

© www.soinside.com 2019 - 2024. All rights reserved.