Facebook SDK swift:获取用户数据的问题(segue)

时间:2015-10-20 12:48:01

标签: ios facebook swift sdk

我的数据似乎不会传递给新的视图控制器。 实际上,它只在登录后的(第一个)视图控制器中起作用。 我相信我的工作中有一些错误。 请告诉我哪里错了,我是初学者所以请准确,高效。非常感谢。

    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

                    }
                }
    }

}

在我的新视图控制器中,我有以下内容:

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.
}
*/

}

1 个答案:

答案 0 :(得分:0)

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

@Bean
public LdapDaoImpl ldapDao(LdapHost ldapHost) {
    LdapDaoImpl ldapDao = new LdapDaoImpl();
    ldapDao.setLdapUrl(ldapHost);
    return ldapDao;
}