如何在iOS中登陆FBSDKLoginKit后获取用户信息?

时间:2022-01-10 01:23:51

I have created a button:

我创建了一个按钮:

let button = FBSDKLoginButton(frame: CGRectMake(100, 100, 300, 50))
button.readPermissions = ["public_profile", "user_friends"]
view.addSubview(button)

then tap it and make some things to login. Where do I receive data after login? How to get access to them?

然后点击它,让一些东西登录。登入后在哪里接收资料?如何访问它们?

NOTE

请注意

The question is about FBSDKLoginKit, not Facebook-ios-sdk.

问题是关于FBSDKLoginKit,而不是Facebook-ios-sdk。

4 个解决方案

#1


1  

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

        if ((error) != nil)
        {
            print("Error: \(error)")
        }
        else
        {
            let uniqueId = result.valueForKey("id") as! NSString!
            let firstName = result.valueForKey("first_name") as! NSString!
            let lastName = result.valueForKey("last_name") as! NSString!
            print(uniqueId) // This works
            print(firstName)
            print(lastName)
        }
    })
}

#2


0  

Implementing the loginButton:didCompleteWithResult:error: delegate method of FBSDKLoginKit should give you access to the login result, as follows:

实现loginButton:didCompleteWithResult:error: FBSDKLoginKit的委托方法应该允许您访问登录结果,如下所示:

class ViewController: UIViewController, FBSDKLoginButtonDelegate {
    override func viewDidLoad() {
        super.viewDidLoad()

        let button = FBSDKLoginButton(frame: CGRectMake(100, 100, 300, 50))
        button.delegate = self
        button.readPermissions = ["public_profile", "user_friends"]
        view.addSubview(button)
    }

    func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError!) {
        // do stuff with result, get token, userId...
    }

    func loginButtonDidLogOut(loginButton: FBSDKLoginButton!) {

    }
}

#3


0  

Simple implementation for received token and userID after login:

登录后收到的令牌和userID的简单实现:

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

    if error != nil {
        print("error")
    } else if result.isCancelled {
        print("cancelled")
    } else {
        print("token: \(result.token.tokenString)")
        print("user_id: \(result.token.userID)")
    }
}

#4


0  

You have to make a graph request to fetch user data. You can get the details here.

您必须创建一个图形请求来获取用户数据。你可以在这里得到细节。

#1


1  

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

        if ((error) != nil)
        {
            print("Error: \(error)")
        }
        else
        {
            let uniqueId = result.valueForKey("id") as! NSString!
            let firstName = result.valueForKey("first_name") as! NSString!
            let lastName = result.valueForKey("last_name") as! NSString!
            print(uniqueId) // This works
            print(firstName)
            print(lastName)
        }
    })
}

#2


0  

Implementing the loginButton:didCompleteWithResult:error: delegate method of FBSDKLoginKit should give you access to the login result, as follows:

实现loginButton:didCompleteWithResult:error: FBSDKLoginKit的委托方法应该允许您访问登录结果,如下所示:

class ViewController: UIViewController, FBSDKLoginButtonDelegate {
    override func viewDidLoad() {
        super.viewDidLoad()

        let button = FBSDKLoginButton(frame: CGRectMake(100, 100, 300, 50))
        button.delegate = self
        button.readPermissions = ["public_profile", "user_friends"]
        view.addSubview(button)
    }

    func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError!) {
        // do stuff with result, get token, userId...
    }

    func loginButtonDidLogOut(loginButton: FBSDKLoginButton!) {

    }
}

#3


0  

Simple implementation for received token and userID after login:

登录后收到的令牌和userID的简单实现:

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

    if error != nil {
        print("error")
    } else if result.isCancelled {
        print("cancelled")
    } else {
        print("token: \(result.token.tokenString)")
        print("user_id: \(result.token.userID)")
    }
}

#4


0  

You have to make a graph request to fetch user data. You can get the details here.

您必须创建一个图形请求来获取用户数据。你可以在这里得到细节。