我正在尝试创建一个流式应用,用于流式传输Spotify中的歌曲。用户只有在他/她登录其帐户时才能流式传输。为此,我创建了两个选项
如果手机上的Spotify应用用户打开应用进行身份验证 用户,完成后调用我的应用程序以允许用户流式播放歌曲
如果用户没有Spotify应用,请在手机上打开safari浏览器 允许用户进行身份验证,然后在完成后调用我的应用程序。
问题
选项2完美无故障,允许用户使用浏览器登录,完成后调用我的应用程序。 选项1从我的应用程序实例化/调用Spotify本机应用程序以允许用户登录。用户完成后,它会调用(Spotify app)我的应用程序的另一个新实例,而不是调用启动Spotify应用程序的旧实例
我做过或尝试过的事情
LSApplicationQueriesSchemes
是否正确,即spotify-action 代码
的AppDelegate
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
//Spotify
setupSpotify()
}
public func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
//Check if this URL was sent from the Spotify app or website
if SPTAuth.defaultInstance().canHandle(url) {
//Send out a notification which we can listen for in our sign in view controller
NotificationCenter.default.post(name: NSNotification.Name.Spotify.authURLOpened, object: url)
return true
}
return FBSDKApplicationDelegate.sharedInstance().application(
app,
open: url as URL!,
sourceApplication: options[UIApplicationOpenURLOptionsKey.sourceApplication] as! String,
annotation: options[UIApplicationOpenURLOptionsKey.annotation]
)
}
func setupSpotify() {
SPTAuth.defaultInstance().clientID = SPOTIFY_CLIENT_ID
SPTAuth.defaultInstance().redirectURL = URL(string: SPOTIFY_CALLBACK_URL)
SPTAuth.defaultInstance().sessionUserDefaultsKey = "spotifySessionKey"
//SPTAuth.defaultInstance().tokenSwapURL = URL(string: SPOTIFY_TOKEN_SWAP_URL)
//SPTAuth.defaultInstance().tokenRefreshURL = URL(string: SPOTIFY_TOKEN_REFRESH_URL)
//For this application we just want to stream music, so we will only request the streaming scope
SPTAuth.defaultInstance().requestedScopes = [SPTAuthStreamingScope]
// Start the player (this is only needed for applications that's using streaming
do {
spotifyPlayer = SPTAudioStreamingController.sharedInstance()
try spotifyPlayer.start(withClientId: SPOTIFY_CLIENT_ID)
} catch {
fatalError("Couldn't start Spotify SDK")
}
}
调用/启动Spotify登录的UIController
func spotify() {
let appURL = SPTAuth.defaultInstance().spotifyAppAuthenticationURL()!
let webURL = SPTAuth.defaultInstance().spotifyWebAuthenticationURL()!
// Before presenting the view controllers we are going to start watching for the notification
NotificationCenter.default.addObserver(self,
selector: #selector(receievedUrlFromSpotify(_:)),
name: NSNotification.Name.Spotify.authURLOpened,
object: nil)
//Check to see if the user has Spotify installed
if SPTAuth.supportsApplicationAuthentication() {
//Open the Spotify app by opening its url
if #available(iOS 10.0, *) {
UIApplication.shared.open(appURL, options: [:], completionHandler: nil)
} else {
// Fallback on earlier versions
UIApplication.shared.openURL(appURL)
}
} else {
spotifyAuthWebView = SFSafariViewController(url: webURL)
//Present a web browser in the app that lets the user sign in to Spotify
present(spotifyAuthWebView, animated: true, completion: nil)
}
}
func receievedUrlFromSpotify(_ notification: Notification) {
spinner = AppUtility.displaySpinner(onView: self.view)
guard let url = notification.object as? URL else { return }
// Close the web view if it exists
if spotifyAuthWebView != nil {
spotifyAuthWebView?.dismiss(animated: true, completion: nil)
}
// Remove the observer from the Notification Center
NotificationCenter.default.removeObserver(self,
name: NSNotification.Name.Spotify.authURLOpened,
object: nil)
SPTAuth.defaultInstance().handleAuthCallback(withTriggeredAuthURL: url) { (error, session) in
//Check if there is an error because then there won't be a session.
if let error = error {
// Pass our error onto another method which will determine how to show it
self.displayErrorMessage(error: error)
return
}
// Check if there is a session
if let session = session {
let sessionData = NSKeyedArchiver.archivedData(withRootObject: session)
//Store spotify session for use later
self.userDefault.set(sessionData, forKey: MUSIC_STREAM_TOKEN)//"SpotifyAuthSession"
self.userDefault.synchronize()
SPTAudioStreamingController.sharedInstance().delegate = self
// If there is use it to login to the audio streaming controller where we can play music.
SPTAudioStreamingController.sharedInstance().login(withAccessToken: session.accessToken)
}
}
}
两个应用实例的场景拍摄
由于