Open simonmitchell opened 2 years ago
Thank you for the workaround snippet here, can confirm this allows for a connection in 8.7.90.459 as well. Trying to guess at the situation that would cause this sort of behavior - some sort of time window of availability for the now-backgrounded apps socket being open? Some sort of interplay of background modes and networking on the iOS SDK side?
FWIW I'm seeing this work about half the time in SDK version 1.2.3 and the Spotify app version 8.8.15.609. Here's my basic code:
AppDelegate:
func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
if (url.absoluteString.contains("spotify/authorized")) {
self.recordingPlayer?.authorizeSpotify(url: url)
return true
}
}
RecordingPlayer:
func authorizeSpotify(url: URL) {
let parameters = self.spotifyRemote?.authorizationParameters(from: url)
if let token = parameters?[SPTAppRemoteAccessTokenKey] {
self.spotifyRemote?.connectionParameters.accessToken = token
self.spotifyRemote?.connect()
let test = self.spotifyRemote?.isConnected
self.spotifyRemote?.playerAPI?.delegate = self
self.spotifyRemote?.playerAPI?.subscribe()
self.spotifyRemote?.delegate = self
} else if let errorDescription = parameters?[SPTAppRemoteErrorDescriptionKey] {
NSLog("Spotify authentication error: " + errorDescription)
}
}
About half the time, the test
variable is true and the subsequent lines work; the other half the time, the test
variable is false and the delegate functions aren't called.
Since the problem only happens when the Spotify app was not already running, it seems like it's calling back to our apps too soon, rather than getting suspended before our apps can connect to it. Could this be fixed by the Spotify app waiting until it is ready to receive a connection before reopening our apps?
SDK Version: Latest release Spotify Version: 8.7.64
Example code:
Delegate methods:
If the spotify app is running/playing music everything works great ✅
If the spotify app is force-quit/not running then the spotify app is launched, connects, starts playing, and my app is re-opened, but I don't get told if the process was successful, and
playerAPI
is never madenon-nil
on mySPTAppRemote
instance.I have found a workaround for this however, which is to at point 2.c in my comments add a foreground observer and re-call
connect()
once the app enters the foreground!This seems to work for now, but I do not trust it in the slightest, and I'm unsure what kind of state I'll end up in if something in the spotify app fails e.t.c (user isn't logged in, doesn't have premium e.t.c.)
Can anyone advise if:
Thanks everyone! 😄