【问题标题】:didInititate method for Spotify IOS SDK is not calling even though called sessionManager.initiateSession()即使调用了 sessionManager.initiateSession(),Spotify IOS SDK 的 didInitiate 方法也没有调用
【发布时间】:2020-06-19 05:21:54
【问题描述】:

我正在通过 Spotify 的身份验证过程,并请求我的应用程序的范围 appRemoteControl 来控制音乐和当前歌曲的 userReadCurrentlyPlaying。我设置了 SPTConfiguration、SPTSessionManager 和 SPTAppRemote 中的所有内容,以及它们所需的委托方法(SPTAppRemoteDelegate、SPTSessionManagerDelegate、SPTAppRemotePlayerStateDelegate),并在用户按下按钮但我无法获取方法时使用请求的范围启动会话

func sessionManager(manager: SPTSessionManager, didInitiate session: SPTSession) {
    appRemote.connectionParameters.accessToken = session.accessToken
    appRemote.connect()
    print(session.accessToken)
}

触发。身份验证过程完全有效,因为它进入我的 spotify 应用程序并返回到我的应用程序并从 configuration.playURI = "" 播放一首歌曲,但是,上面的方法永远不会被调用。我关注了 spotify 演示项目,但仍然无法正常工作。这是我的完整代码

class LogInViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
}

let spotifyClientID = Constants.clientID
let spotifyRedirectURL = Constants.redirectURI

let tokenSwap = "https://***********.glitch.me/api/token"
let refresh = "https://***********.glitch.me/api/refresh_token"

lazy var configuration: SPTConfiguration = {
    let configuration = SPTConfiguration(clientID: spotifyClientID, redirectURL: URL(string: "Lyrically://callback")!)
    return configuration
}()

lazy var sessionManager: SPTSessionManager = {
    let manager = SPTSessionManager(configuration: configuration, delegate: self)
    if let tokenSwapURL = URL(string: tokenSwap), let tokenRefreshURL = URL(string: refresh) {
        self.configuration.tokenSwapURL = tokenSwapURL
        self.configuration.tokenRefreshURL = tokenRefreshURL
        self.configuration.playURI = ""
    }
    return manager
}()

lazy var appRemote: SPTAppRemote = {
    let appRemote = SPTAppRemote(configuration: configuration, logLevel: .debug)
    appRemote.delegate = self
    return appRemote
}()

@IBAction func logIn(_ sender: UIButton) {
    let requestedScopes: SPTScope = [.appRemoteControl, .userReadCurrentlyPlaying]
    sessionManager.initiateSession(with: requestedScopes, options: .default)
}

}

extension LogInViewController: SPTAppRemotePlayerStateDelegate {
func playerStateDidChange(_ playerState: SPTAppRemotePlayerState) {
    print("state changed")
}

}

extension LogInViewController: SPTAppRemoteDelegate {
func appRemoteDidEstablishConnection(_ appRemote: SPTAppRemote) {
    print("connected")
    appRemote.playerAPI?.delegate = self
    appRemote.playerAPI?.subscribe(toPlayerState: { (success, error) in
        if let error = error {
            print("Error subscribing to player state:" + error.localizedDescription)
        }
    })
}

func appRemote(_ appRemote: SPTAppRemote, didFailConnectionAttemptWithError error: Error?) {
    print("failed")
}

func appRemote(_ appRemote: SPTAppRemote, didDisconnectWithError error: Error?) {
    print("disconnected")
}

}

extension LogInViewController: SPTSessionManagerDelegate {

func sessionManager(manager: SPTSessionManager, didInitiate session: SPTSession) {
    appRemote.connectionParameters.accessToken = session.accessToken
    appRemote.connect()
    print(session.accessToken)
}

func sessionManager(manager: SPTSessionManager, didFailWith error: Error) {
    print("failed",error)
}

}

【问题讨论】:

    标签: swift spotify


    【解决方案1】:

    想通了。必须通过创建一个实例从 LogInViewController 中获取 sessionManager

    lazy var logInVC = LogInViewController()
    

    然后将这行代码添加到场景委托中的openURLContexts方法中

    func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
        print("Opened url")
        guard let url = URLContexts.first?.url else {
           return
        }
        logInVC.sessionManager.application(UIApplication.shared, open: url, options: [:])
    }
    

    【讨论】:

    • 我也有同样的问题。我已将您在上面发布的代码添加到 sceneDelegate 类中,但行为没有改变。想知道你能不能帮我解决这个问题!
    • 当然!我最终做的是将处理 spotify 登录过程的所有内容移到 SceneDelegate 中。我看到了 Spotify 提供的一个 Spotify IOS 演示项目,我决定朝这个方向前进。然后在 sceneDelegate 方法 openURLContexts 中,我添加了 sessionManager.application(UIApplication.shared, open: url, options: [:]) 允许我的应用程序远程连接
    • 知道了。那么对于新的 Spotify SDK,AppDelegate openURL 方法实际上已被弃用?
    • 是的。不再使用,而是使用sceneDelegate方法openURLContexts。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-31
    • 1970-01-01
    • 2020-01-30
    • 2013-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多