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

标签 ios facebook swift sdk

我的数据似乎没有传递到新的 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/

相关文章:

facebook - 在 Javascript 中从 uid 获取 Facebookthird_party_id

facebook graph api..获取 friend 的 friend

ios - 使用 TabBarController 自定义 NavigationBar 高度

ios - 我们可以在 iOS 上检测到非 iBeacon 信标吗?

ios - iOS 模拟器中的 3+ 多点触控

ios - 即使在手动安装证书后也无法在 iOS 模拟器上通过 SSL 握手

swift - 动态修复屏幕中的 UICollectionView 单元格

ios - reloadData 不适用于 UITableView 数据源?

facebook - 七层 hell (Feed 问题、照片发布问题等)

ios - Swift/NSURLSession JSON 下载/在展开可选值时意外发现 nil