gpt4 book ai didi

ios - Facebook SDK swift : problems with fetching user data (segue)

转载 作者:行者123 更新时间:2023-11-30 13:58:51 28 4
gpt4 key购买 nike

我的数据似乎没有传递到新的 View Controller 。事实上,它仅在登录后在(第一个) View Controller 中工作。我相信我的工作中存在一些错误。请告诉我哪里错了,我是初学者,所以请准确而高效。非常感谢。

    class ViewController: UIViewController, FBSDKLoginButtonDelegate
{

override func viewDidLoad()
{
super.viewDidLoad()


if FBSDKAccessToken.currentAccessToken() != nil {
//user already has access token
self.logUserData()
} else {

loginButton.readPermissions = ["public_profile", "email", "user_friends"]

loginButton.delegate = self


self.Photo.image = UIImage(named: "Why subtle bckd image1")
self.Photo2.image = UIImage(named: "Un combo unique pict")

}
}

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




// MARK: PR/VAR


var firstName: String!
var lastName: String!
var email: String!



@IBOutlet var loginButton: FBSDKLoginButton!

@IBAction func loginButtonsend(sender: UIButton) {

sender.setTitle("firstName", forState: UIControlState.Normal)
sender.setTitle("lastName", forState: UIControlState.Normal)
sender.setTitle("email", forState: UIControlState.Normal)
}



@IBOutlet var Photo: UIImageView!
@IBOutlet var Photo2: UIImageView!






// MARK: FACEBOOK LOGIN


override func viewWillAppear(animated: Bool) {
self.logUserData()

}


func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError!)
{ if error == nil {

let fbAccessToken = FBSDKAccessToken.currentAccessToken().tokenString
println("Logged in")

} else { println(error.localizedDescription) } }



func loginButtonDidLogOut(loginButton: FBSDKLoginButton!)
{
println("User logged out.")

}

func logUserData()
{
let graphRequest = FBSDKGraphRequest(graphPath: "me?fields=id,email,first_name,last_name", parameters: ["fields": "first_name,email,last_name"])
graphRequest.startWithCompletionHandler { (connection, result, error) -> Void in

if error != nil
{
// Process error
println(error)
}
else
{
println(result.grantedPermissions)
println("fetched user = \(result)")
var firstName = result.valueForKey("first_name")
println("firstName = \(firstName)")
var lastName = result.valueForKey("last_name")
println("lastName is = \(lastName)")
var email = result.valueForKey("email")
println("email is = \(email)")

self.performSegueWithIdentifier("showNew", sender: self)
}

}

}
// SEGUE:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

if (segue.identifier == "showNew") {

if let destinationVC = segue.destinationViewController as? NewViewController {
destinationVC.firstName = firstName
destinationVC.lastName = lastName
destinationVC.email = email

}
}
}

}

在我的新 View Controller 中,我有以下内容:

class NewViewController: UIViewController {

var firstName: String!
var lastName: String!
var email: String!


@IBOutlet var firstNameLabel: UILabel!
@IBOutlet var lastNameLabel: UILabel!
@IBOutlet var emailLabel: UILabel!


override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)

if (firstName != nil) {
firstNameLabel.text = firstName
}

if (lastName != nil) {
lastNameLabel.text = lastName
}

if (email != nil) {
emailLabel.text = email

}

}

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





@IBAction func logoutButtonTapped(sender: AnyObject) {





let loginManager = FBSDKLoginManager ()

loginManager.logOut()





let loginPage = self.storyboard?.instantiateViewControllerWithIdentifier("ViewController") as ViewController



//?

let loginPageNav = UINavigationController (rootViewController: loginPage)





let appDelegate = UIApplication.sharedApplication().delegate as AppDelegate



appDelegate.window?.rootViewController = loginPageNav



}



/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/

}

最佳答案

似乎您从未在 ViewController 中设置变量,而是创建了三个与您应该在 logUserData 上使用的变量同名的变量。只需更改为:

println(result.grantedPermissions)
println("fetched user = \(result)")
firstName = result.valueForKey("first_name")
println("firstName = \(firstName)")
lastName = result.valueForKey("last_name")
println("lastName is = \(lastName)")
email = result.valueForKey("email")
println("email is = \(email)")

关于ios - Facebook SDK swift : problems with fetching user data (segue),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33237112/

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