I am trying to implement Spotify integration with current user's playlist to display it in my tableview. I have integrated with login and access token everything works fine. I have gone through stack overflow link:- How to get the list of songs using Spotify in Swift3 iOS? but didn't work for me.
Then to get print for canonicalUsername as below, its showing nil value
SPTUser.requestCurrentUser(withAccessToken:(SPTAuth.defaultInstance().session.accessToken)!) { (error, data) in
guard let user = data as? SPTUser else { print("Couldn't cast as SPTUser"); return }
let userId = user.canonicalUsername
})
I have even tried this link Spotify iOS SDK Swift display all (!) playlists (20+) due to beginner may be it also didn't work for me. Is there any way to get Spotify's current user-id? How could I show the current user's playlist in my table view?
Just go through online tutorial in youtube :- https://www.youtube.com/watch?v=KLsP7oThgHU&t=1s for latest version in 2019.
Download full source code with Spotify Integration + search options + default Spotify url and fetch current user's playlist and play in our native iOS App
Source:- https://github.com/azeemohd786/Spotify-Demo
Based on your question the solution to get print canonicalUsername or current user's id try as below,
SPTUser.requestCurrentUser(withAccessToken: session.accessToken) { (error, data) in
guard let user = data as? SPTUser else { print("Couldn't cast as SPTUser"); return }
let userID = user.canonicalUserName
print(userID!)
}
Then to get current user's playlist and play in ur device, first call the SPT Delegate in your view controller like and then function call,
class PlayVC: UIViewController, SPTAudioStreamingDelegate, SPTAudioStreamingPlaybackDelegate {
func audioStreamingDidLogin(_ audioStreaming: SPTAudioStreamingController) {
let playListRequest = try! SPTPlaylistList.createRequestForGettingPlaylists(forUser: session.canonicalUsername, withAccessToken: session.accessToken)
Alamofire.request(playListRequest)
.response { response in
let list = try! SPTPlaylistList(from: response.data, with: response.response)
for playList in list.items {
if let playlist = playList as? SPTPartialPlaylist {
print( playlist.name! ) // playlist name
print( playlist.uri!) // playlist uri
// self.tableView.reloadData()// if u want to display playlist name and other stuffs like so..
SPTAudioStreamingController.sharedInstance().playSpotifyURI("\(playlist.uri!)", startingWith: 0, startingWithPosition: 10) { error in
if error != nil {
print("*** failed to play: \(error)")
return
}
}
}}}
}
}