本文介绍了在Swift上,如何使用Facebook登录并使用FBSDK解析时如何检索名称和其他数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过Parse使用Facebook登录时,无法获取用户名或电子邮件.我应该在AppDelegate中正确设置其他所有内容.

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.

当我使用电子邮件登录时,我的User类起作用,并且可以使用我向其注册的数据.当我尝试通过Facebook登录时,我只获得了长字母数字字符串作为用户名并停止.我想取回名字,照片,出生地和城市.

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.

在我的User.swift文件中,我做到了:

import Foundation

struct User
{
    let username : String
    let address : String
}

这是我的登录按钮:

@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.")
            }
        }
    }

也尝试过此操作,但不起作用:

    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)")
        }
    }

预先感谢

推荐答案

这是我用来通过Parse获取Facebook信息的有效代码.使用user.isNew成功进行身份验证后,将调用该函数.即使用户不是新用户,也可以调用它,以确保他们重新登录时确保您拥有最新信息.

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)
                    }
                })
            }
        }

    }
}

这篇关于在Swift上,如何使用Facebook登录并使用FBSDK解析时如何检索名称和其他数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 14:37