spotify / ios-sdk

Spotify SDK for iOS
https://developer.spotify.com/documentation/ios/
661 stars 188 forks source link

How do I connect without starting playback? #343

Open ir-fuel opened 2 years ago

ir-fuel commented 2 years ago

I am at loss here, can't find what to do.

I want to connect to the Spotify app, but do playback operations later. Right now:

So how can I make a connection to Spotify without starting a song?

zolthan commented 2 years ago

Hi @ir-fuel did you find a solution? I would be glad if you would share it with us. Best Z

ir-fuel commented 2 years ago

Seems the only way is using SPTSessionManager but then you need to build a backend for 2 webservices. Doing this as we speak.

zolthan commented 2 years ago

Would it be possible to start playing and immediately stop it using AppRemote?

ir-fuel commented 2 years ago

You can try, but worst case it would make a small sound when doing this. It's not a very solid solution.

zolthan commented 2 years ago

Would you mind sharing your Session Solution once you’ve finished it?

Von meinem iPhone gesendet

Am 04.01.2022 um 12:36 schrieb ir-fuel @.***>:

 You can try, but worst case it would make a small sound when doing this. It's not a very solid solution.

— Reply to this email directly, view it on GitHub, or unsubscribe. Triage notifications on the go with GitHub Mobile for iOS or Android. You are receiving this because you commented.

ir-fuel commented 2 years ago

Ok, so I got it working with the SPTSessionManager, however I see no way of persisting this.

So, what are we supposed to do:

So far so good. The problem however is this: SPTSessionManager contains the session attribute, which contains:

  accessToken
  refreshToken
  expirationDate
  scope
  expired

Which is all we need to have an open session, and when the token expires request a new one.

However! There is no way to persist this session attribute or to initialize an SPTSessionManager with an existing accessToken and refreshToken, so what does this mean?

This all works fine as long as the app you wrote is open (foreground / background). As soon as it is killed this object is erased from memory, and the data in the session object is not persisted in any way. The result is that next time you start your app SPTSessionManager contains an empty session, and you will need to re-run the authorization flow. This does not ask the user permission in Spotify since it has already given that, but it does switch your app to the Spotify app and back to yours to do this, leading to a suboptimal user experience. I don't even see how to use the refresh token in a correct way in this scenario. From what I can tell the access token is valid for one hour.

Also there are zero examples of how to use the refresh token, without annoying the user. The example code just uses appRemote connect and disconnect calls, which as documented somewhere, only work if the Spotify app is actually playing a song. Just check the SPTLoginSampleAppSwift example project to see what I am talking about. The delegate for refresh is implemented (it contains a log statement), but it's absolutely not clear how and when that is called, especially since after the initial access token request, the session manager is no longer used anywhere else in the code.

TBH I'm pretty disappointed with all this. I would have hoped that a company like Spotify would have invested more effort in this.

On the other hand, that also explains the weird behaviour I see when using Spotify in Waze. It all makes sense now.

griffinbaker12 commented 1 year ago

Store the session in user defaults / keychain and then renew if necessary to avoid app re-directs.