当我将应用程序与Facebook连接时,我无法显示用户的姓名?

时间:2015-07-06 12:28:43

标签: ios swift facebook-login

我试图让facebook登录我的应用程序与facebook连接,一切顺利,我创建了一个登录页面添加了一个imageView来显示用户facebook的个人资料照片,一个标签显示当我进入Facebook时,我设法显示用户的图像,但是我无法将名称的标签连接到代码以显示用户名!有人可以帮忙吗?这是我的代码:

import UIKit
import FBSDKCoreKit
import FBSDKShareKit
import FBSDKLoginKit

class ViewController: UIViewController,FBSDKLoginButtonDelegate {

@IBOutlet weak var nameLabel: UILabel!

@IBOutlet weak var profileImageView: FBSDKProfilePictureView!

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

    if (FBSDKAccessToken.currentAccessToken() != nil)
    {
        // User is already logged in, do work such as go to next view controller.
    }
    else
    {
        let loginView : FBSDKLoginButton = FBSDKLoginButton()
        self.view.addSubview(loginView)
        loginView.center = self.view.center
        loginView.readPermissions = ["public_profile", "email", "user_friends"]
        loginView.delegate = self

    } 
}

// Facebook Delegate Methods

func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError!) {
    println("User Logged In")

    if ((error) != nil)
    {
        // Process error
    }
    else if result.isCancelled {
        // Handle cancellations
    }
    else {
        // If you ask for multiple permissions at once, you
        // should check if specific permissions missing
        if result.grantedPermissions.contains("email")
        {
            // Do work
        }
    }
}

func loginButtonDidLogOut(loginButton: FBSDKLoginButton!) {
    println("User Logged Out")
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
func returnUserData()
{
    let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: nil)
    graphRequest.startWithCompletionHandler({ (connection, result, error) -> Void in

        if ((error) != nil)
        {
            // Process error
            println("Error: \(error)")
        }
        else
        {
            println("fetched user: \(result)")
            let userName : NSString = result.valueForKey("name") as! NSString
            println("User Name is: \(userName)")
            let userEmail : NSString = result.valueForKey("email") as! NSString
            println("User Email is: \(userEmail)")
        }
    })
}

}

2 个答案:

答案 0 :(得分:0)

如果您浏览调试器中的代码,userName是否设置为用户的名称?我在代码中的任何地方都没有看到您实际将UILabel nameLabel的文本设置为您的userName var。

你能提供更多细节吗?如果这只是一个疏忽,问题是你需要将nameLabel的文本设置为userName。

nameLabel.text = userName

如果这不是疏忽而且userName没有设置为用户名的正确值,那么您很可能在Facebook SDK中做错了什么。

答案 1 :(得分:0)

不确定你是否想出了这个问题,但我遇到了同样的问题,我有个人资料图片显示而不是用户名。我刚刚添加了最后一行' self。'你的代码,它工作:

let userName : NSString = result.valueForKey("name") as! NSString
            print("Username is: \(userName)")
            self.nameResult.text = userName as String 

swap' nameResult'对于您的标签名称' nameLabel'这应该有用。