spotify / ios-sdk

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

How to automatically reconnect app remote after Spotify app resumes playback. #347

Open jadenbanson opened 2 years ago

jadenbanson commented 2 years ago

My app uses the app remote to subscribe to the players state.

But when iOS terminates the Spotify app in the background after another app plays media, then restarts it after I leave that media, app remote becomes disconnected (via "end of stream").

Right now, my only solution is sending the user a push notification telling them to open my app and press a button to reconnect to app remote, which is bad for UX.

Is there anyway to automatically reconnect app remote when the Spotify app resumes playback?

jayeshtejwani8055 commented 2 years ago
func appRemote(_ appRemote: SPTAppRemote, didDisconnectWithError error: Error?) {
        reconnectSpotify()
    }

    func reconnectSpotify() {
        DispatchQueue.main.asyncAfter(deadline: .now() + 1.5) {
            if !self.appRemote.isConnected {
                self.appRemote.connectionParameters.accessToken = self.accessToken
                self.appRemote.connect()
                self.appRemote.authorizeAndPlayURI("")
            }
        }
    }