问题
Can't get the user's name or email when login with Facebook via Parse. I should have set everything else properly in the AppDelegate.
When I login with my email, my User class works, and can use data I registered with. When I try to login via Facebook, I only got the long alphanumerical string as username and stop. I'd like to retrive name, foto, birth and city.
In my User.swift file I'got this:
import Foundation
struct User
{
let username : String
let address : String
}
This is my login button:
@IBAction func facebookLoginAction(sender: UIButton)
{
PFFacebookUtils.logInInBackgroundWithReadPermissions(["public_profile", "user_about_me", "user_birthday"]) {
(user: PFUser?, error: NSError?) -> Void in
if let user = user
{
if user.isNew
{
println("User signed up and logged in through Facebook!")
}
else
{
println("User logged in through Facebook!")
}
self.dismissViewControllerAnimated(true, completion: nil)
}
else
{
println("Uh oh. The user cancelled the Facebook login.")
}
}
}
tried this too, but doesn't work:
func getUserInfo() {
// if let session = PFFacebookUtils.session() {
if let session = PFFacebookUtils.facebookLoginManager() {
if session.isOpen {
println("session is open")
FBRequestConnection.startForMeWithCompletionHandler({ (connection: FBRequestConnection!, result: AnyObject!, error: NSError!) -> Void in
//println("done me request")
if error != nil {
println("facebook me request - error is not nil :(")
} else {
println("facebook me request - error is nil :) ")
let urlUserImg = "http://graph.facebook.com/\(result.objectID)/picture?type=large"
let firstName = result.first_name
let lastName = result.last_name
}
})
}
} else {
//let user:PFUser = PFUser.currentUser()
//println("ohooo \(user)")
}
}
thanks in advance
回答1:
This is working code I use to get Facebook information with Parse. The function is called after successful authentication with user.isNew. It can also be called even if the user isn't new to make sure you have the most up-to-date information when they log back in.
func loadFacebookData() {
let graphRequest = FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "email, name, id"])
graphRequest.startWithCompletionHandler { (connection, result, error) -> Void in
if error != nil {
let error = error!.userInfo["error"] as! String
}
else {
if let userName = result.valueForKey("name") as? String, email = result.valueForKey("email") as? String, id = result.valueForKey("id") as? String {
let pictureURL: NSURL = NSURL(string: "https://graph.facebook.com/\(id)/picture?type=large&return_ssl_resources=1")!
let user = PFUser.currentUser()!
let query = PFUser.query()
query!.whereKey("email", equalTo: email)
query!.getFirstObjectInBackgroundWithBlock({ (oldUser: PFObject?, error) -> Void in
if error != nil && oldUser != nil {
let error = error!.userInfo["error"] as! String
}
else {
self.setFacebookInfo(user, userEmail: email, userName: userName, pictureURL: pictureURL)
}
})
}
}
}
}
来源:https://stackoverflow.com/questions/33065748/on-swift-how-to-retrive-name-and-other-data-when-login-with-facebook-and-parse