Spotify iOS sdk 使用现有访问令牌连接到 Spotify 远程应用程序
Posted
技术标签:
【中文标题】Spotify iOS sdk 使用现有访问令牌连接到 Spotify 远程应用程序【英文标题】:Spotify iOS sdk connect to spotify app remote with existing access token 【发布时间】:2020-12-28 12:37:21 【问题描述】:我正在制作一个带有颤振的应用程序,我想在给定 uri 的情况下使用 spotify 播放一首歌曲。我已经在我的 dart 代码中使用 spotify web api 实现了身份验证,所以我已经有了一个有效的访问令牌。我需要使用 ios sdk 来播放带有 uri 的曲目,所以我需要快速实现它。我想要做的是将访问令牌传递给 swift 代码并将 spotify 应用程序远程对象与访问令牌连接,并跳过spotify iOS sdk quick start guide 的“设置用户授权”部分下的身份验证步骤。这是我完整的 AppDelegate.swift 代码
import UIKit
import Flutter
import Firebase
@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate, SPTAppRemoteDelegate, SPTAppRemotePlayerStateDelegate
let SpotifyClientID = client id
let SpotifyRedirectURL = redirect uri
lazy var configuration = SPTConfiguration(
clientID: SpotifyClientID,
redirectURL: SpotifyRedirectURL
)
private let spotifyMethodChannelName = "spotify"
private var spotifyAppRemote: SPTAppRemote? = nil
private var result: FlutterResult? = nil
func appRemoteDidEstablishConnection(_ appRemote: SPTAppRemote)
print("connected")
self.result!("success")
func appRemote(_ appRemote: SPTAppRemote, didDisconnectWithError error: Error?)
print("disconnected")
func appRemote(_ appRemote: SPTAppRemote, didFailConnectionAttemptWithError error: Error?)
print("failed " + error.debugDescription)
func playerStateDidChange(_ playerState: SPTAppRemotePlayerState)
print("player state changed")
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool
// FirebaseApp.configure()
let controller : FlutterViewController = window?.rootViewController as! FlutterViewController
let spotifyChannel = FlutterMethodChannel(name: spotifyMethodChannelName,
binaryMessenger: controller.binaryMessenger)
spotifyChannel.setMethodCallHandler(
(call: FlutterMethodCall, result: @escaping FlutterResult) -> Void in
// Note: this method is invoked on the UI thread.
self.result = result
switch(call.method)
case "connect":
let args: Dictionary<String, AnyObject> = (call.arguments as? Dictionary<String, AnyObject>)!
self.connect(accessToken: args["accessToken"] as! String)
case "playTrack":
let args: Dictionary<String, AnyObject> = (call.arguments as? Dictionary<String, AnyObject>)!
self.playTrack(uri: args["uri"] as! String)
default:
result(FlutterError(code: "METHOD CALL DOESN'T EXIST",
message: "Method call doesn't exist",
details: nil))
)
GeneratedPluginRegistrant.register(with: self)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
private func connect(accessToken: String) -> Void
print(accessToken)
spotifyAppRemote = SPTAppRemote(configuration: self.configuration, logLevel: .debug)
spotifyAppRemote!.connectionParameters.accessToken = accessToken
spotifyAppRemote!.delegate = self
spotifyAppRemote!.connect()
private func playTrack(uri: String) -> Void
self.spotifyAppRemote?.playerAPI?.play(uri, asRadio: false, callback: peram1,peram2 in)
self.result!("success")
虽然相关部分是这个连接函数
private func connect(accessToken: String) -> Void
print(accessToken)
spotifyAppRemote = SPTAppRemote(configuration: self.configuration, logLevel: .debug)
spotifyAppRemote!.connectionParameters.accessToken = accessToken
spotifyAppRemote!.delegate = self
spotifyAppRemote!.connect()
如果 Spotify 应用当前正在后台播放音乐,此代码将成功连接到 Spotify 应用,否则将无法连接(即使 Spotify 在后台打开但不播放音乐)。快速入门指南使用一种名为 authorizeAndPlayUri 的方法来播放曲目,但我不想授权,因为我已经有一个有效的访问令牌。即使没有使用访问令牌在后台打开,我如何连接到 spotify 应用程序?这也是我第一次使用 iOS 原生代码,所以我绝对有可能犯了一个我没有看到的与 swift 相关的错误。
【问题讨论】:
【参考方案1】:您可以尝试包含一个道具 playURI: "", 在你的配置里面
来源:https://github.com/cjam/react-native-spotify-remote/issues/75
【讨论】:
【参考方案2】:好吧,根据官方 Spotify sdk 文档,我们不走运。如果 Spotify 应用当前未运行,您需要使用 authorizeAndPlayURI 来唤醒它。
https://spotify.github.io/ios-sdk/html/Classes/SPTAppRemote.html#//api/name/connect
编辑: 您可以使用 checkIfSpotifyAppIsActive 来检查 Spotify 应用程序当前是否在后台处于活动状态,以决定简单地连接它或使用 authorizeAnPlayURI 来唤醒应用程序。
https://spotify.github.io/ios-sdk/html/Classes/SPTAppRemote.html#//api/name/checkIfSpotifyAppIsActive:
预先获取访问令牌的好处是,如果 Spotify 应用程序恰好在运行,则不会有任何应用程序切换,事情会更顺畅。否则,应用切换并继续。
【讨论】:
以上是关于Spotify iOS sdk 使用现有访问令牌连接到 Spotify 远程应用程序的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Spotify SDK for Android 上刷新访问令牌?