Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/118.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 登录Spotify后,如何创建、保存和传递有效的SPCSession?_Ios_Swift_Core Data_Spotify_Spotify App - Fatal编程技术网

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

Ios 登录Spotify后,如何创建、保存和传递有效的SPCSession?,ios,swift,core-data,spotify,spotify-app,Ios,Swift,Core Data,Spotify,Spotify App,我在Spotify beta 9上遇到了问题。所有关于保存SPCSession和使用RefreshTokenURL更新(刷新)的教程似乎都被淘汰了。这就是我获取AuthViewController的方式 let spotifyAuthenticationViewController = SPTAuthViewController.authenticationViewController() spotifyAuthenticationViewController.d

我在Spotify beta 9上遇到了问题。所有关于保存SPCSession和使用RefreshTokenURL更新(刷新)的教程似乎都被淘汰了。这就是我获取AuthViewController的方式

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

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

您必须将其存储在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];
}

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

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