Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SPTAppRemote checkIfSpotifyAppIsActive always returns false if Spotify app has been restarted since last connection #438

Open
Fump opened this issue Sep 18, 2024 · 8 comments

Comments

@Fump
Copy link

Fump commented Sep 18, 2024

We use to SPTAppRemote checkIfSpotifyAppIsActive to check if the Spotify app is running in the background. If it is isActive we use the SPTAppRemote and attempt to connect and observe current playback for already authorized users. I have noticed that if the Spotify app has been restarted since the last time we were connected to the SPTAppRemote then checkIfSpotifyAppIsActive always returns false in its completion handler. The expected behaviour is that checkIfSpotifyAppIsActive always would return true if the Spotify is running in the background.

I'm fairly certain that this worked for us before the summer (June 2024), so I'm suspecting either a regression due to iOS 18 or a new Spotify app update. I have tested to revert to an older SDK version (2.1.1) but still having the issue.

Steps to reproduce:

  1. Force close the Spotify app
  2. Open Spotify app and start playing music
  3. Call SPTAppRemote checkIfSpotifyAppIsActive and completion handler is called with isActive false.

After we have had an succesful connection with the SPTAppRemote checkIfSpotifyAppIsActive returns expected values until Spotify app is re-launched again.

Spotify SDK version: 2.1.7
Spotify App version: 8.9.76
iOS version: 18.0
Xcode version: 16.0

@Fump
Copy link
Author

Fump commented Sep 26, 2024

To provide some additional information, connection attempts to the SPTAppRemote also fails in this state even though the Spotify app is running and playing music in the background.

Error print:
AppRemote: Failed connection attempt with error: Error Domain=com.spotify.app-remote.transport Code=-2000 "Stream error." UserInfo={NSLocalizedDescription=Stream error., NSUnderlyingError=0x3035fa760 {Error Domain=NSPOSIXErrorDomain Code=61 "Connection refused" UserInfo={_kCFStreamErrorCodeKey=61, _kCFStreamErrorDomainKey=1}}, NSLocalizedRecoverySuggestion=Reconnect the transport to the Spotify app., NSLocalizedFailureReason=A stream error has occured in the transport.}

For reference we are using SPTSessionManager to authorise the user and receive tokens used to try to connect. We have verified that the token is valid and not expired when attempting to connect. (Refreshing session doesn't help). The same access token works fine to connect to the SPTAppRemote once we have done an app jump by calling appRemote.authorizeAndPlayURI.

@cwalo
Copy link

cwalo commented Sep 26, 2024

I am observing the same behavior in 2.1.7. I recently upgraded from 1.2.4 where it was working reliably. FWIW, I don't believe this is an iOS regression as my test device is still on iOS 17.6.1.

@Fump
Copy link
Author

Fump commented Sep 27, 2024

@cwalo Are you sure it worked on 1.2.4 with latest Spotify IOS app version? I just tried downgrading our SpotifyiOS swift package version to 1.2.4 but I am still seeing the same issues with SPTAppRemote checkIfSpotifyAppIsActive returning false even if Spotify is running in background playing music.

If it's working on iOS 17.6.1 I would suspect it's an Spotify App update regression.

@cwalo
Copy link

cwalo commented Sep 27, 2024

@Fump Wow, actually, you're correct. I downgraded back to 1.2.5 and see the same behavior there. Not sure how long it's been broken, but looks like I didn't notice because I have a fallback that polls the web api. For me, trying to connect with appRemote.connect() after receiving an access_token will log:

AppRemote: Connecting...
nw_socket_handle_socket_event [C1.1.1:1] Socket SO_ERROR [61: Connection refused]
nw_socket_handle_socket_event [C1.1.2:1] Socket SO_ERROR [61: Connection refused]
nw_connection_get_connected_socket_block_invoke [C1] Client called nw_connection_get_connected_socket on unconnected nw_connection
TCP Conn 0x30322c1e0 Failed : error 0:61 [61]

No errors are returned in the delegate callback:
func appRemote(_ appRemote: SPTAppRemote, didFailConnectionAttemptWithError error: Error?) {

@cwalo
Copy link

cwalo commented Oct 4, 2024

Well that answers that https://github.com/spotify/ios-sdk/releases/tag/v3.0.0

Remove checkIfSpotifyAppIsActive. It won't work with new versions of the Spotify iOS app.

@Fump
Copy link
Author

Fump commented Oct 5, 2024

Too bad, but at least it’s a clear answer to why it’s not working anymore. Thanks for notifying @cwalo !

@jalopezcar would it be possible to get some guidance on how we can continue to support connecting to the Spotify app without an app jump if it’s already playing music in the background? When we already have a valid user auth token that is. Or is that flow simply not supported anymore after this change?

@cwalo
Copy link

cwalo commented Oct 11, 2024

I alluded to it (why I didn't notice this was broken), but here's what I've been doing:

  • restore a persisted SPTSession (using NSKeyedArchiver/Unarchiver) since the SDK doesn't do this for us
  • if we have a session, set SPTSessionManager.session, refresh the session/token if needed (i have a token swap service to avoid needing to call authorizeAndPlayURI)
    • else call SPTSessionManager.initiateSession
  • make an initial request to /me/player/ to get the current player state
  • if PlayerState.device is "smartphone", attempt appRemote.connect
    • else poll /me/player/ (here you might instead call authorizeAndPlay)

Note: I'm not currently using the SDK for remote control, just getting the player state.

@addy239
Copy link

addy239 commented Oct 20, 2024

While we understand now the the checkIfSpotifyAppIsActive won't work anymore (thanks for the workaround @cwalo), but do we have any solution on why the appRemote.connect only works after the app jump, but won't connect even though Spotify is running in the background?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants