Search code examples
iosswiftfacebook-sdk-4.0

Data persistence in GraphRequest with FBSDK


Good afternoon community,

I'm trying to get the data from graphrequest and be able to pass the data to another viewController, but apparently it doesn't get saved in a variable, outside of making the query, the data is removed, it doesn't get saved in a variable.

Have any of you had this problem or something like that?

I leave my code below of my first viewController

import UIKit
import FBSDKLoginKit

class ViewController: UIViewController,FBSDKLoginButtonDelegate {

@IBOutlet var test: UILabel!


var comprobacion = "";

override func viewDidLoad() {
    super.viewDidLoad()
    if let token = FBSDKAccessToken.current(), !token.isExpired {

        let loginButton = FBSDKLoginButton()
        loginButton.center = view.center
        loginButton.layoutIfNeeded()
        loginButton.delegate = self
        view.addSubview(loginButton)
        loginButton.awakeFromNib()
        loginButton.readPermissions = ["public_profile", "email"]
        
    }else{
        
    let loginButton = FBSDKLoginButton()
    loginButton.center = view.center
    loginButton.delegate = self
    view.addSubview(loginButton)
    loginButton.layoutIfNeeded()
    loginButton.awakeFromNib()
    loginButton.readPermissions = ["public_profile", "email"]
    
    }
    
}

func loginButton(_ loginButton: FBSDKLoginButton!, didCompleteWith result: FBSDKLoginManagerLoginResult!, error: Error!) {
    
    let resultado = result?.token?.tokenString
    
    let request = FBSDKLoginKit.FBSDKGraphRequest(graphPath: "me", parameters: ["fields":"email,name"], tokenString: resultado, version: nil, httpMethod: "get")
    
    request?.start(completionHandler: { connection , result, error in
        
        let datos: [String:AnyObject] = result as! [String:AnyObject];
        
        self.comprobacion =  (datos["email"] as! NSString) as String;
        
        print("campos \(String(describing: result) )")
        
        self.test.text = self.comprobacion
        
    })
    
    performSegue(withIdentifier: "second", sender: self)
    let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
    let newViewController = storyBoard.instantiateViewController(withIdentifier: "SecondController") as! SecondController
    self.present(newViewController, animated: true, completion: nil)
    
    
     
    
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    let destino = segue.destination as! SecondController
    destino.recibir = comprobacion;
    
}


func loginButtonDidLogOut(_ loginButton: FBSDKLoginButton!) {
    
    print("GoodBye...   Hommi")
}
}

I leave my code below of my second viewController

 import UIKit

class SecondController: UIViewController {

@IBOutlet var Email: UILabel!
var recibir: String!



override func viewDidLoad() {
    super.viewDidLoad()
    print("recibir \(String(describing: recibir))")
  
    Email.text = String(recibir)
}

 }

I realized that the email is not saved in my variable, since it is outside the clousure request.start if I print the variable it is empty.

some help?


Solution

  • Re-arrange code to

    func loginButton(_ loginButton: FBSDKLoginButton!, didCompleteWith result: FBSDKLoginManagerLoginResult!, error: Error!) {
        
        let resultado = result?.token?.tokenString
        
        let request = FBSDKLoginKit.FBSDKGraphRequest(graphPath: "me", parameters: ["fields":"email,name"], tokenString: resultado, version: nil, httpMethod: "get")
        
        request?.start(completionHandler: { connection , result, error in
            
            let datos: [String:AnyObject] = result as! [String:AnyObject];
            
            self.comprobacion =  (datos["email"] as! NSString) as String;
            
            print("campos \(String(describing: result) )")
            
            self.test.text = self.comprobacion 
    
            DispatchQueue.main.async {
               self.performSegue(withIdentifier: "second", sender: self) 
            }
            
        })
        
        
    }
    

    Tips

    1- Code inside request?.start(completionHandler runs asynchnoulsy after the segue so the segue occurs while your var is still nil

    2- Don't instantiate a vc and present it when you already use a segue