gpt4 book ai didi

swift - 从一个 View Controller 获取 Facebook 值(电子邮件、姓名等)并将它们传输到另一个 View Controller

转载 作者:行者123 更新时间:2023-11-28 08:16:12 25 4
gpt4 key购买 nike

所以我目前正在设计一个应用程序并进行帐户注册。我已经实现了 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.
}

下一个 View Controller ,一个典型的注册页面,有这些文本字段:

@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 获取值并将它们放入这些文本输入框中。我是编程新手,非常感谢任何帮助,谢谢!

最佳答案

首先,将 FB 值存储在您以后可以访问的变量中。

接下来,您可以在转换之前设置下一个 View 的值。例如,如果你正在使用一个 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
}

关于swift - 从一个 View Controller 获取 Facebook 值(电子邮件、姓名等)并将它们传输到另一个 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42541497/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com