SPTAppRemote checkIfSpotifyAppIsActive always returns false if Spotify app has been restarted since last connection
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:
- Force close the Spotify app
- Open Spotify app and start playing music
- Call
SPTAppRemote checkIfSpotifyAppIsActiveand 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
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.
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.
@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.
@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?) {
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.
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?
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 callauthorizeAndPlayURI)- else call
SPTSessionManager.initiateSession
- else call
- make an initial request to
/me/player/to get the current player state - if
PlayerState.deviceis "smartphone", attemptappRemote.connect- else poll
/me/player/(here you might instead callauthorizeAndPlay)
- else poll
Note: I'm not currently using the SDK for remote control, just getting the player state.
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?