gpt4 book ai didi

ios - 在 View Controller 之间传递 FBSDK 登录管理器结果

转载 作者:行者123 更新时间:2023-11-30 12:26:55 24 4
gpt4 key购买 nike

我无法在 View Controller 之间传输登录管理器结果,Segue 与按钮相关联,其标识符为 s1。我的设置是正确的。程序因绿色断点而崩溃。这是我的代码:对于第一个 VC:

import FBSDKLoginKit


class ViewController: UIViewController {

var user_name: String?

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}


override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let fbLoginManager = FBSDKLoginManager()
fbLoginManager.logIn(withReadPermissions: ["public_profile", "email"], from: self) { (result, error) in
if let error = error {
print("Failed to login: \(error.localizedDescription)")
return
}

guard let accessToken = FBSDKAccessToken.current() else {
print("Failed to get access token")
return
}

let credential = FIRFacebookAuthProvider.credential(withAccessToken: accessToken.tokenString)


FBSDKGraphRequest(graphPath: "/me", parameters: ["fields" : "email, id, locale"])
.start(completionHandler: { (connection, result, error) in
guard let result = result as? NSDictionary,
let user_name = result["user_name"] as? String,

else {

return
}

if(segue.identifier == "s1"){
if let v = segue.destination as? Re {


v.uname=user_name ?? ""



//v.uname = usr.text ?? ""
}
}


})


// Perform login by calling Firebase APIs
FIRAuth.auth()?.signIn(with: credential, completion: { (user, error) in
if let error = error {
print("Login error: \(error.localizedDescription)")
let alertController = UIAlertController(title: "Login Error", message: error.localizedDescription, preferredStyle: .alert)

return
}



})

}


}


}

对于 Re,下一个 VC:

class Re: UIViewController {
var uname: String?
@IBOutlet weak var l1: UILabel!
var userfb: String?
override func viewDidLoad() {
super.viewDidLoad()
l1.text=uname
// Do any additional setup after loading the view.
}

}

最佳答案

您需要使用您在顶部声明的实例变量,如下所示。现在您已经创建了新的 user_name 并使用另一个 user_name

guard let result = result as? NSDictionary,
user_name = result["user_name"] as? String,// make change here

else {

return
}

关于ios - 在 View Controller 之间传递 FBSDK 登录管理器结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44067284/

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