spotify / ios-sdk

Spotify SDK for iOS
https://developer.spotify.com/documentation/ios/
659 stars 187 forks source link

Accessing ViewController UI inside "didInitiate" function after login #98

Open andrenaught opened 5 years ago

andrenaught commented 5 years ago

Mac OS Version: Mojave - 10.14.3 XCode Version: 10.1 Tested on both: ios 12.1 (iphone 8) & ios 10.3.3 (iphone 5c)

I managed to get the authentication to work, everything works I can get data from the user using their access token.

My only issue is trying to update the UI values on the current view controller after successful authentication, my UI Label display_name becomes nil after the app returns from the authentication phase in Spotify app or SFSafariViewController. Is there any way to update the display_name within didInitiate?

The weird part is that it works perfectly on the simulation: DispatchQueue.main.async works, but not on real devices

// In my ViewController.swift
class ViewController: UIViewController, SPTSessionManagerDelegate {
    @IBOutlet weak var display_name: UILabel!

    //protocol stubs for 'SPTSessionManagerDelegate'
    func sessionManager(manager: SPTSessionManager, didInitiate session: SPTSession)
    {
        print("success", session)

        DispatchQueue.main.async {
            self.display_name.text = "data from api" // Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value
        }
    }
...
// In my AppDelegate.swift
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    lazy var rootViewController = ViewController()
    func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
        rootViewController.sessionManager.application(app, open: url, options: options)
        print("application func")
        return true
    }
...
satishkoladiya commented 4 years ago

Any solutions?

kkarayannis commented 4 years ago

You probably haven't connected the var to the label in the storyboard