Spotify Session Management

Ich habe ein Spotify-Login in meiner App und versuche, eine automatische Anmeldung durchzuführen:

Login Funktion

func getSpotifyToken(fromController controller: UIViewController, success: (spotifyToken: String?) -> Void, failure: (error: NSError?) -> Void) {

    loginSuccessBlock = success
    loginFailureBlock = failure

    SPTAuth.defaultInstance().clientID        = SpotifyClientID
    SPTAuth.defaultInstance().redirectURL     = NSURL(string: SpotifyRedirectURI)
    SPTAuth.defaultInstance().requestedScopes = [SPTAuthStreamingScope, SPTAuthPlaylistReadPrivateScope]

    let spotifyLoginController = SPTAuthViewController.authenticationViewController()
    spotifyLoginController.delegate = self
    spotifyLoginController.clearCookies { () -> Void in
        controller.presentViewController(spotifyLoginController, animated: true, completion: nil)
    }
}

Prüfen ob Sitzung existiert

private func spotifyConnected() -> Bool {        
    if SPTAuth.defaultInstance().session == nil {
        self.loadSpotifySession()
    }        
    return SPTAuth.defaultInstance().session != nil
}

Sitzung sicher

private func saveSpotifySession() {
    let sessionData = NSKeyedArchiver.archivedDataWithRootObject(SPTAuth.defaultInstance().session)
    NSUserDefaults.standardUserDefaults().setObject(sessionData, forKey: Spotify_Session_Key)
    NSUserDefaults.standardUserDefaults().synchronize()
}

Sitzung laden

private func loadSpotifySession() {        
    if let sessionData = NSUserDefaults.standardUserDefaults().objectForKey(Spotify_Session_Key) as? NSData {
        let session = NSKeyedUnarchiver.unarchiveObjectWithData(sessionData) as! SPTSession
        SPTAuth.defaultInstance().session = session
    }
}

Sitzung erneuern - bei App-Start aufrufen

func renewSpotifySession() {        
    guard spotifyConnected() else {
        return
    }

    SPTAuth.defaultInstance().renewSession(SPTAuth.defaultInstance().session) { (error: NSError!, session: SPTSession!) -> Void in
        if session != nil {
            SPTAuth.defaultInstance().session = session                
        } else {
            print("Failed to refresh spotify session")
        }
    }        
}

renewSession return nil. Ich habe einige Informationen über refreshToken gesehen, aber ich weiß nicht, wo ich sie finden kann.

Wie kann ich die Spotify-Sitzung verlängern? Vielleicht habe ich etwas falsch gemacht?

Antworten auf die Frage(2)

Ihre Antwort auf die Frage