【问题标题】:Facebook SDK and FirebaseFacebook SDK 和 Firebase
【发布时间】:2017-11-02 07:19:33
【问题描述】:

我想从Facebook 检索用户的数据并将该特定数据存储在firebase 中并在UILabel 上显示它

如果有,请提供任何有用的链接。

我的代码:

  fileprivate  func setupFacebookButton() {

        // add facebook button
        let loginButton = FBSDKLoginButton()

        loginButton.frame = CGRect(x: 24, y:  535, width: 165, height: 44)
        loginButton.delegate = self
        loginButton.readPermissions = ["email", "public_profile" , "user_birthday", "user_location"]
        view.addSubview(loginButton)
    }

let accessToken = FBSDKAccessToken.current()
guard let accessTokenString = accessToken?.tokenString else { return }
let credentials = FacebookAuthProvider.credential(withAccessToken: accessTokenString)
Auth.auth().signIn(with: credentials, completion: { (user, error) in
    if error != nil {
        print("Something went wrong with our FB user: ", error ?? "")
        return
    }

    print("Successfully logged in with our user: ", user ?? "")
})
let requestParameters =  ["fields": "name, email, birthday, gender, location"]


//location first_name, gender, age_range, user_birthday
FBSDKGraphRequest(graphPath: "/me", parameters: requestParameters).start { (connection, result, err) in
    if err != nil {
            print("Failed to start graph request:", err ?? "")
            return
        }
print(result ?? "")
    }

输出

{
    birthday = "09/21/1987";
    email = "user@mail.ru";
    gender = male;
    id = 1520497814669566;
    location =     {
        id = 106065422766757;
        name = "User city, User Country";
    };
    name = "User Name";
}
Successfully logged in with our user:  <FIRUser: 0x6080000c87a0>

【问题讨论】:

  • 你想要存储来自 Facebook 的哪些信息?是整个result 还是只是某些数据?
  • @EmilDavid 只有整个结果

标签: ios swift facebook firebase uilabel


【解决方案1】:

我不会为你做所有事情,而是给你提示和指示。

从您的问题来看,您似乎还没有设置 Firebase。在继续阅读之前,请先按照these 的步骤操作,并查看他们的示例以了解如何构建数据库。

1.类创建

我会将所有这些数据封装到一个名为Person 的类中,如下所示。

struct Location
{
    var id: Int = ''
    var name: String = ''
}

class Person
{
    var id: Int = 0
    var name: String = ''
    var email: String = ''
    var gender: String = ''
    var birthday: String = ''
    var location: Location = Location()

    init(id: Int, name: String, email: String, gender: String, birthday: String, location: Location)
    {
        self.id = id
        self.name = name
        self.email = email
        self.gender = gender
        self.birthday = birthday
        self.location = location
    }

    // TODO: Finish implementing this
    init(snapshot: FIRDataSnapshot)
    {
        // This will make it easier to creating objects once you retrieve data from Firebase
    }

    // TODO: Finish implementing this
    var dictionary: [String:Any]
    {
        return
        [
            // This will make it easier to create a dictionary out of your object
        ]
    }
}

查看this 问题,看看他们如何实现init(snapshot: FIRDataSnapshot) 构造函数并尝试实现你的。重要的是,您首先要了解他们的数据库树的外观。

对于var dictionary 部分,this 的答案肯定会帮助您实现您的答案。

2。 Facebook结果理解

就像大多数 API 一样,Facebook 查询返回 JSON。 JSON 只是一个键/值对,这意味着它们可以存储为字典。

我建议你先尝试摆弄result 以便理解它。

要提取用户名,您必须使用let userName = result.valueForKey("name") as! String

请注意选角。我将其转换为String,因为从结果来看,它是String。对于id,您必须改为转换为Int

如果您查看location,它内部包含另一个字典。我会让你弄清楚如何从那里提取信息。

3.火力基地

希望当您在这里熟悉 Firebase 中的读/写操作时。 您可能已经注意到,为了将数据保存到 Firebase,您需要给它一个字典。这就是var dictionary 变量的作用。阅读this问题的答案和cmets,了解如何使用它及其相关性。

4.总结

完成上述所有步骤后,就可以将它们拼凑起来了。

  • 从 Facebook 提取内容
  • 从中创建一个Person 对象
  • object.dictionary 传递到 Firebase 的写入函数中。
  • 瞧! 观察你的数据库填充;这是最重要的部分:)

愉快的编码。干杯队友:)

【讨论】:

  • 非常感谢!
  • @FaigHuseynov 不客气。如果它有助于解决您的问题并允许在 SO 上关闭问题,请接受作为答案
  • 这个方法适合uisegmentcontrol吗?使用细分,我定义性别(男/女)
  • 是的。只需使用selectedSegmentIndex 属性获取当前选定的段,然后将其值分配给对象的age。使用类的主要思想是它们是通用的。只要你有数据,就可以在任何地方使用
猜你喜欢
  • 2021-07-27
  • 1970-01-01
  • 2017-08-02
  • 1970-01-01
  • 2014-11-22
  • 1970-01-01
  • 1970-01-01
  • 2013-09-15
  • 2017-09-17
相关资源
最近更新 更多