从一个视图控制器获取Facebook值(电子邮件,名称等)并将它们转移到另一个视图控制器

时间:2017-03-01 20:32:26

标签: swift xcode facebook swift3 facebook-php-sdk

所以我目前正在设计一个应用程序,正在进行帐户注册。我已经实现了Facebook登录,但是我无法获取用户的Facebook信息并将其转移到下一页,理想情况下,名称和电子邮件的字段将填充从Facebook的。

Facebook登录的初始页面如下所示:

class RegisterVC: UIViewController, FBSDKLoginButtonDelegate {

override func viewDidLoad() {
    super.viewDidLoad()

    let loginButton = FBSDKLoginButton()
    view.addSubview(loginButton)
    loginButton.frame = CGRect(x: 82, y: 325, width: view.frame.width - 210, height: 59)

    loginButton.delegate = self
}

func loginButtonDidLogOut(_ loginButton: FBSDKLoginButton!) {
    print("Did log out of facebook")
}

func loginButton(_ loginButton: FBSDKLoginButton!, didCompleteWith result: FBSDKLoginManagerLoginResult!, error: Error!) {
    if error != nil {
        print(error)
        return
    }
    print("Successfully logged in")

    FBSDKGraphRequest(graphPath: "/me", parameters: ["fields": "id, name, email"]).start {(connection, result, err) in
        if err != nil {
            print("Failed to start graph request", err)
            return
        } else {

            guard let data = result as? [String:Any] else {return}

            let fbEmail = data["email"]
            let fbName  = data["name"]
        }

        print(result)
    }

}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    //get destination view an set the fullname

        let vc = segue.destination as? CreateAccountVC
        vc?.email = self.fbEmail
        vc.fullname = self.fbName

}
override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

下一个视图控制器(典型的注册页面)包含以下文本字段:

@IBOutlet weak var fullname: UITextField!
@IBOutlet weak var username: UITextField!
@IBOutlet weak var age: UITextField!
@IBOutlet weak var email: UITextField!
@IBOutlet weak var verifyEmail: UITextField!
@IBOutlet weak var password: UITextField!
@IBOutlet weak var verifyPassword: UITextField!

我不知道如何从Facebook获取值并将它们放入这些文本输入框中。我对编程非常陌生,所以任何帮助都会非常感谢,谢谢!

1 个答案:

答案 0 :(得分:0)

首先,将FB值存储在稍后可以访问的var中。

接下来,您可以在转换之前为下一个视图设置值。例如,如果您使用的是segue,则可以在prepareForSegue中执行此操作:

class RegisterVC: UIViewController, FBSDKLoginButtonDelegate {

var fbName:String?

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  //get destination view an set the fullname
  let vc=segue.destination as? MyCustomVC
  vc.fullName=self.fbName 
}