-
-
Save Jeverett3000/0088bef4b0da7e2f9262ba75a5079120 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import UIKit | |
| class SceneDelegate: UIResponder, UIWindowSceneDelegate, | |
| SPTAppRemoteDelegate { | |
| static private let kAccessTokenKey = "access-token-key" | |
| private let redirectUri = URL(string:"comspotifytestsdk://")! | |
| private let clientIdentifier = "089d841ccc194c10a77afad9e1c11d54" | |
| var window: UIWindow? | |
| lazy var appRemote: SPTAppRemote = { | |
| let configuration = SPTConfiguration(clientID: self.clientIdentifier, redirectURL: self.redirectUri) | |
| let appRemote = SPTAppRemote(configuration: configuration, logLevel: .debug) | |
| appRemote.connectionParameters.accessToken = self.accessToken | |
| appRemote.delegate = self | |
| return appRemote | |
| }() | |
| var accessToken = UserDefaults.standard.string(forKey: kAccessTokenKey) { | |
| didSet { | |
| let defaults = UserDefaults.standard | |
| defaults.set(accessToken, forKey: SceneDelegate.kAccessTokenKey) | |
| } | |
| } | |
| func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) { | |
| guard let url = URLContexts.first?.url else { | |
| return | |
| } | |
| let parameters = appRemote.authorizationParameters(from: url); | |
| if let access_token = parameters?[SPTAppRemoteAccessTokenKey] { | |
| appRemote.connectionParameters.accessToken = access_token | |
| self.accessToken = access_token | |
| } else if let _ = parameters?[SPTAppRemoteErrorDescriptionKey] { | |
| // Show the error | |
| } | |
| } | |
| func sceneDidBecomeActive(_ scene: UIScene) { | |
| connect(); | |
| } | |
| func sceneWillResignActive(_ scene: UIScene) { | |
| playerViewController.appRemoteDisconnect() | |
| appRemote.disconnect() | |
| } | |
| func connect() { | |
| playerViewController.appRemoteConnecting() | |
| appRemote.connect() | |
| } | |
| // MARK: AppRemoteDelegate | |
| func appRemoteDidEstablishConnection(_ appRemote: SPTAppRemote) { | |
| self.appRemote = appRemote | |
| playerViewController.appRemoteConnected() | |
| } | |
| func appRemote(_ appRemote: SPTAppRemote, didFailConnectionAttemptWithError error: Error?) { | |
| print("didFailConnectionAttemptWithError") | |
| playerViewController.appRemoteDisconnect() | |
| } | |
| func appRemote(_ appRemote: SPTAppRemote, didDisconnectWithError error: Error?) { | |
| print("didDisconnectWithError") | |
| playerViewController.appRemoteDisconnect() | |
| } | |
| var playerViewController: ViewController { | |
| get { | |
| let navController = self.window?.rootViewController?.children[0] as! UINavigationController | |
| return navController.topViewController as! ViewController | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment