Spotify iOS应用程序中设置令牌会禁用登录回调

我正在尝试使用Spotify的SDK设置我的iOS应用的登录名.我有登录工作,但只有没有令牌.一旦我添加了这两行代码
SPTAuth.defaultInstance().tokenSwapURL = NSURL(string: kTokenSwapURL)
SPTAuth.defaultInstance().tokenRefreshURL = NSURL(string: kTokenRefreshServiceURL)

登录不起作用这是我的登录代码.

AppDelegate.swift

let kClientID = "my-client-id"
let kCallbackURL = "my-callback-url"
let kTokenSwapURL = "my-token-swap-url"
let kTokenRefreshServiceURL = "my-token-refresh-url"

func application(application: UIApplication,didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.

    // Override point for customization after application launch.
    SPTAuth.defaultInstance().clientID = kClientID
    SPTAuth.defaultInstance().redirectURL = NSURL(string: kCallbackURL)
    SPTAuth.defaultInstance().requestedScopes = [SPTAuthStreamingScope,SPTAuthUserReadPrivateScope,SPTAuthPlaylistReadPrivateScope]
    SPTAuth.defaultInstance().sessionUserDefaultsKey = "SpotifySession"

    window = UIWindow(frame: UIScreen.mainScreen().bounds)

    let loginViewController = LoginViewController(nibName: "LogInViewController",bundle: nil)
    let navigationController = UINavigationController(rootViewController: loginViewController)

    window?.rootViewController = navigationController
    window?.makeKeyAndVisible()

    return true
}

func application(application: UIApplication,openURL url: NSURL,sourceApplication: String?,annotation: AnyObject) -> Bool {
    let authCallback : SPTAuthCallback = { error,session in
        // This is the callback that'll be triggered when auth is completed (or fails).

        if (error != nil) {
            print(error);
            return;
        }

        let userDefaults = NSUserDefaults.standardUserDefaults()
        let sessionData = NSKeyedArchiver.archivedDataWithRootObject(session)
        userDefaults.setobject(sessionData,forKey: SPTAuth.defaultInstance().sessionUserDefaultsKey)
        userDefaults.synchronize()

        AuthHandler.sharedHandler.loginWithSession(session)
    };

    if SPTAuth.defaultInstance().canHandleURL(url) {
        SPTAuth.defaultInstance().handleAuthCallbackWithTriggeredAuthURL(url,callback:authCallback)
        return true
    }

    return false;
}

LoginViewController.swift

class LoginViewController: UIViewController {

    let kClientID = "my-client-id"
    let kCallbackURL = "my-callback-url"
    let kTokenSwapURL = "my-token-swap-url"
    let kTokenRefreshServiceURL = "my-token-refresh-url"


    var session: SPTSession!

    var logIn: UIButton!

    var auth : SPTAuthViewController?

    override func viewWillAppear(animated: Bool) {
        // set login callback for what happens when session is got
        AuthHandler.sharedHandler.setLoginCallback({ success in
            if (success) {
                self.transitionToPlaylistScreen()
            }
        })

        // if session is still valid,login
        let userDefaults = NSUserDefaults.standardUserDefaults()

        if let sessionObj:AnyObject = userDefaults.objectForKey("SpotifySession") { // session available
            let sessionDataObj = sessionObj as! NSData

            let session = NSKeyedUnarchiver.unarchiveObjectWithData(sessionDataObj) as! SPTSession

            if !session.isValid() {
                SPTAuth.defaultInstance().renewSession(session,callback: { (error:NSError!,renewdSession:SPTSession!) -> Void in
                    if error == nil {
                        let sessionData = NSKeyedArchiver.archivedDataWithRootObject(session)
                        userDefaults.setobject(sessionData,forKey: SPTAuth.defaultInstance().sessionUserDefaultsKey)
                        userDefaults.synchronize()

                        self.session = renewdSession
                        AuthHandler.sharedHandler.loginWithSession(self.session!)
                    } else {
                        print(error.localizedDescription)
                    }
                })
            } else {
                self.session = session
                AuthHandler.sharedHandler.loginWithSession(self.session!)
            }
        }
    }

    override func viewDidLoad() {
        // add observer for login success
        NSNotificationCenter.defaultCenter().addobserver(self,selector: Selector("transitionToPlaylistScreen"),name: "loginSuccess",object: nil)

       // code to set up the login button
    }

    func transitionToPlaylistScreen() {
        if (self.auth != nil) {
            self.dismissViewControllerAnimated(true,completion: nil)
            self.auth = nil
        }
        let playlistScreen = PlaylistViewController()
        let navigation = UINavigationController(rootViewController: playlistScreen)
        dispatch_async(dispatch_get_main_queue(),{
            self.presentViewController(navigation,animated: true,completion: nil)
        })
    }

    func loginToSpotify() {
        // if session isn't valid,login within app
        dispatch_async(dispatch_get_main_queue(),{
            self.auth = SPTAuthViewController.authenticationViewController()
            self.auth?.delegate = AuthHandler.sharedHandler
            self.auth!.modalPresentationStyle = .OverCurrentContext
            self.auth!.modalTransitionStyle = .Crossdissolve
            self.modalPresentationStyle = .CurrentContext
            self.definesPresentationContext = true
            self.auth!.clearCookies({
                dispatch_async(dispatch_get_main_queue(),{
                    self.presentViewController(self.auth!,animated: false,completion: nil)
                })
            })
        })
    }
}

AuthHandler.swift

class AuthHandler: NSObject,SPTAuthViewDelegate {
    static let sharedHandler = AuthHandler()

    var session: SPTSession?

    var callback: (Bool -> Void)?

    func setLoginCallback(callback: (Bool -> Void)) {
        self.callback = callback
    }

    func authenticationViewController(authenticationViewController: SPTAuthViewController!,didFailToLogin error: NSError!) {
        if let function = callback {
            function(false)
        }
    }

    func authenticationViewController(authenticationViewController: SPTAuthViewController!,didLoginWithSession session: SPTSession!) {
        self.loginWithSession(session)
    }

    func authenticationViewControllerDidCancelLogin(authenticationViewController: SPTAuthViewController!) {
        if let function = callback {
            function(false)
        }
    }

    func loginWithSession(session: SPTSession) {
        self.session = session
        SPTAuth.defaultInstance().session = session
        if let function = callback {
            function(true)
        }
    }
}

解决方法

我猜你的后端(交换/刷新)服务器没有正确设置,因为不工作的服务器将导致登录失败.

我推荐this repository,你可以在heroku上设置一个简单的服务器.

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...