swift - Spotify 续订 session fatal error : unexpectedly found nil while unwrapping an Optional value

标签 swift spotify access-token option-type

正在尝试从 Spotify 续订 session ,以便我可以检索新的访问权限并刷新 token 。从第一行打印开始,我收到 fatal error 。

@IBAction func renewSession(sender: AnyObject) {

    let auth = SPTAuth.defaultInstance()
    auth.tokenRefreshURL = NSURL(string: tokenRefresh)
    auth.tokenSwapURL = NSURL(string: tokenSwap)
    auth.session = self.spotifySession
    auth.sessionUserDefaultsKey = sessionKey

    if (auth.session != nil) {

        SPTAuth.defaultInstance().renewSession(SPTAuth.defaultInstance().session, callback: {(error, session) -> Void in

            if error != nil {

                print("The renewed Spotify session is", session)
                print("The renewed canonical user name in the session is", session.canonicalUsername)
                print("The renewed access Spotify token in session is - %@", auth.session.accessToken)
                print("The renewed encrypted refresh Spotify token in session is - %@", auth.session.encryptedRefreshToken)
                print("The renewed expiration date of the Spotify access token is - %@", auth.session.expirationDate)

            } else {

                print ("The problem with the renewal session is", error)

            }

        })

    }

最佳答案

您应该检查if session != nil,而不是if error != nil。存在一些错误,但您的代码正在将控制转移到不正确的流程。

关于swift - Spotify 续订 session fatal error : unexpectedly found nil while unwrapping an Optional value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34258478/

相关文章:

swift - 如何在 Swift 3 中测试 URL 并获取状态代码?

ios - CIImage 回到 CMSampleBuffer

ios - 视频未在 AVPlayer 中播放

c - Spotify 将 C 更改为 VB.net

c# - 生产服务器上的 ASP.NET Identity session 过期过快

macos - 具有嵌套相对宽度的自动布局性能?

meteor - "415 Error"查询 Spotify token 时

api - 在 Spotify api 中检测用户的当前歌曲

Laravel Passport tokensExpireIn 似乎不起作用

laravel - 如何使用 access_token 对 Laravel 5.4 的 SPA 进行身份验证