2017-05-30 3 views
2

Das Spotify iOS SDK ist alles in objective-c und ab heute sieht es so aus, als ob jede Anfrage, die das SDK benötigt, ein Token erfordert. Wenn ich versuche, einen Song zu suchen, ich diesen Fehler:Festlegen von Autotoken in Spotify iOS SDK

["error": { message = "No token provided"; status = 401; }]

Hier ist mein AppDelegate Code:

var auth = SPTAuth() 
var window: UIWindow? 


func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 
    //Neccessary Spotify stuff 
    auth.redirectURL = URL(string: "my_redirect_url") 
    auth.sessionUserDefaultsKey = "current session" 

    FIRApp.configure() 

    window = UIWindow(frame: UIScreen.main.bounds) 
    window?.makeKeyAndVisible() 
    window?.rootViewController = CustomTabBarController() 

    return true 
} 

// 1 
func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool { 
    // 2- check if app can handle redirect URL 
    if auth.canHandle(auth.redirectURL) { 
     // 3 - handle callback in closure 
     auth.handleAuthCallback(withTriggeredAuthURL: url, callback: { (error, session) in 
      // 4- handle error 
      if error != nil { 
       print("error!") 
      } 
      // 5- Add session to User Defaults 
      let userDefaults = UserDefaults.standard 
      let sessionData = NSKeyedArchiver.archivedData(withRootObject: session as Any) 
      userDefaults.set(sessionData, forKey: "SpotifySession") 
      userDefaults.synchronize() 
      // 6 - Tell notification center login is successful 
      NotificationCenter.default.post(name: Notification.Name(rawValue: "loginSuccessfull"), object: nil) 
     }) 
     return true 
    } 
    return false 
} 

Wie stelle ich das Token in Swift?

Antwort

0
var auth = SPTAuth.defaultInstance()! 
var session:SPTSession! 

Zu allererster Setup-Redirect-URL & Client-ID:

  let redirectURL = "xyz://" // put your redirect URL here 
      auth.redirectURL  = URL(string: redirectURL) 
      auth.clientID  = kClientId 
      auth.requestedScopes = [SPTAuthStreamingScope, SPTAuthPlaylistReadPrivateScope, SPTAuthPlaylistModifyPublicScope, SPTAuthPlaylistModifyPrivateScope] 
      loginUrl = auth.spotifyWebAuthenticationURL() 

Verwenden Sie dann Anmelden Methode:

 if UIApplication.shared.openURL(loginUrl!) { 

      if auth.canHandle(auth.redirectURL) { 
       // To do - build in error handling 
      } 
     } 

Do lassen Sie mich wissen, wenn Sie weitere Hilfe für das gleiche benötigen. Ich habe es in Swift und in meiner aktuellen Anwendung konfiguriert.

Verwandte Themen