使用 FBSDK 在 GraphRequest 中进行数据持久化

Data persistence in GraphRequest with FBSDK

大家下午好,

我正在尝试从 graphrequest 获取数据并能够将数据传递给另一个 viewController,但显然它没有保存在变量中,除了进行查询之外,数据被删除,它不会保存在变量中。

你们中有人遇到过这个或类似的问题吗?

我将我的代码留在第一个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")
}
}

我把我的代码留在我的第二个 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)
}

 }

我意识到电子邮件没有保存在我的变量中,因为它在关闭之外 request.start 如果我打印变量它是空的。

有帮助吗?

将代码重新排列为

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) 
        }
        
    })
    
    
}

提示

1- request?.start(completionHandler 中的代码在 segue 之后异步运行,因此在您的 var 仍为 nil 时发生 segue

2- 不要实例化 vc 并在您已经使用 segue

时呈现它