Spotify Renew Session Fatal error: unexpectedly found nil while unwrapping an Optional value

Spotify Renew Session Fatal error: unexpectedly found nil while unwrapping an Optional value

正在尝试从 Spotify 续订会话,以便我可以检索新的访问和刷新令牌。从第一行打印开始,我收到致命错误。

@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。有一些 error 但您的代码正在将控制移至不正确的流程。