Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Authenticate outside of AppDelegate #151

Closed
Wouter125 opened this issue Sep 15, 2019 · 3 comments
Closed

Authenticate outside of AppDelegate #151

Wouter125 opened this issue Sep 15, 2019 · 3 comments

Comments

@Wouter125
Copy link

Wouter125 commented Sep 15, 2019

I prefer keeping my AppDelegate as empty and clean as possible so after I tested the setup as described in the docs and it worked I planned on moving all the logic out of there.

So I created a file setup, all the managers and pointed my app url open to this new file. And then something weird started to happen. I get the authorization code in the app open. The app open initializes my SPTSessionManager which has everything set from delegates and so on, but the delegate itself is never called, I just get;

Task <4F59CD54-A5D1-43F3-955B-5CCB9C6D9A7C>.<1> finished with error - code: -999

My setup looks like this (SessionManager);

class SpotifySessionManager: NSObject, SPTSessionManagerDelegate {
    lazy var configuration = SPTConfiguration(
        clientID: SpotifyConfiguration.Session.clientID,
        redirectURL: SpotifyConfiguration.Session.redirectURL
    )
    
    lazy var sessionManager: SPTSessionManager = {
        if let tokenSwapURL = SpotifyConfiguration.Session.tokenSwapURL, let tokenRefreshURL = SpotifyConfiguration.Session.tokenRefreshURL {
            self.configuration.tokenSwapURL         = tokenSwapURL
            self.configuration.tokenRefreshURL      = tokenRefreshURL
        }
        
        let manager = SPTSessionManager(configuration: self.configuration, delegate: self)
        
        return manager
    }()
    
    func initiateSpotifySession() {
        let requestedScopes: SPTScope = [.playlistModifyPublic, .playlistModifyPrivate, .ugcImageUpload]
        sessionManager.initiateSession(with: requestedScopes, options: .default)
    }

    
    func sessionManager(manager: SPTSessionManager, didInitiate session: SPTSession) {
        print("Session started", session)
    }
    
    func sessionManager(manager: SPTSessionManager, didFailWith error: Error) {
        print("Failed to refresh spotify session", error)
    }
    
    func sessionManager(manager: SPTSessionManager, didRenew session: SPTSession) {
        print("Session renewed", session)
    }
}

My AppDelegate.swift;

func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
        SpotifySessionManager().sessionManager.application(app, open: url, options: options)
        return true
    }
@Davidorenlane
Copy link

Davidorenlane commented Sep 20, 2019

Hey Wouter,

I did the same thing (but it's working), but my AppDelegate.swift looks like this:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        return true
    }
    
    func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
        sessionOpener.sessionManager.application(app, open: url, options: options)
        return true
    }

I'm not sure if you're problem is related to this, but in your delegate you have:

SpotifySessionManager().**setupManager()**.application(app, open: url, options: options)

which I think should be:
SpotifySessionManager().**sessionManager()**.application(app, open: url, options: options)

Try these fixes out and see if it helps.

@Wouter125
Copy link
Author

Wouter125 commented Sep 22, 2019

Aah thanks for your reply. I was playing around with the setup of the sessionManager and some old code got lost there. I wrote a function to manually build the sessionmanager instead of a lazy var.

Anyways it should be spotifySessionManager().sessionManager.application(app, open: url, options: options). Just a copy + pasta mistake from my end...

Okay, now that I downloaded iOS13 and updated the spotify app it suddenly started working... I didn't change anything, but oh well.

@Davidorenlane
Copy link

Glad to hear it :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants