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
.
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)
}
})
}
Implementing the loginButton:didCompleteWithResult:error:
delegate method of FBSDKLoginKit
should give you access to the login result, as follows:
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!) {
}
}
Simple implementation for received token
and userID
after login:
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)")
}
}
You have to make a graph request to fetch user data. You can get the details here .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.