On Swift, how to retrive name and other data when login with Facebook and Parse with FBSDK

拟墨画扇 提交于 2019-12-25 06:01:05

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!