Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Jerome Heaven
Jerome Heaven
8,641 Points

Getting a cannot convert value of type 'Data' to expected argument type "Data' error in Spotify swift project. Help!

import Foundation
import SafariServices

protocol LoginManagerDelegate: class {
    func loginManagerDidLoginWithSuccess()
}

class LoginManager {

    static var shared = LoginManager()
    fileprivate init() {
        let redirectURL = "AudioMix://returnAfterLogin" 
        let clientID = "924afc65c280462987b83222d1fea17d"
        auth.sessionUserDefaultsKey = "kCurrentSession"
        auth.redirectURL     = URL(string: redirectURL)
        auth.clientID        = clientID
        auth.requestedScopes = [SPTAuthStreamingScope, SPTAuthPlaylistReadPrivateScope, SPTAuthPlaylistModifyPublicScope, SPTAuthPlaylistModifyPrivateScope]
    }


    weak var delegate: LoginManagerDelegate?
    var auth = SPTAuth.defaultInstance()!
    private var session: SPTSession? {
        if let sessionObject = UserDefaults.standard.object(forKey: auth.sessionUserDefaultsKey) as? Data {

// **Getting Error Here**
            return NSKeyedUnarchiver.unarchiveObject(with: sessionObject) as? SPTSession



        }
        return nil
    }
    var isLogged: Bool {
        if let session = session {
            return session.isValid()
        }
        return false
    }

    func preparePlayer() {
        guard let session = session else { return }
        MediaPlayer.shared.configurePlayer(session, id: auth.clientID)
    }

    func login() {
        let safariVC = SFSafariViewController(url: auth.spotifyWebAuthenticationURL())
        UIApplication.shared.keyWindow?.rootViewController?.present(safariVC, animated: true, completion: nil)
    }

    func handled(url: URL) -> Bool {
        guard auth.canHandle(auth.redirectURL) else {return false}
        auth.handleAuthCallback(withTriggeredAuthURL: url, callback: { (error, session) in
            if error != nil {
                print("error!")
            }
            self.delegate?.loginManagerDidLoginWithSuccess()
            self.preparePlayer()
        })
        return true
    }
}