Swift - Firebase在Firestore中获取Facebook好友UID

时间:2018-04-14 14:12:35

标签: swift facebook firebase facebook-graph-api google-cloud-firestore

对于一些小背景 - 我有一个'跟随Facebook好友'的ViewController,用户可以在这里查看已下载我的应用的朋友,以便关注应用。

我已正确调用FBSDK获取已下载该应用的朋友列表,但现在我需要在Firebase中获取这些朋友的内容,以便我可以相应地关注/取消关注它们。

目前,当我获得FB用户ID时 - 我得到了facebook文档中提到的数字字符串。但是,Firebase中的用户uid是不同的。有没有办法找到哪个firebase用户与FB ID相关联?或者我应该尝试通过firebase中的facebook id存储Facebook用户?

    var facebookContacts = [User]()

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "facebookContactCell", for: indexPath) as! FacebookContactCell

    // Configure the cell...
    let user = facebookContacts[indexPath.row]

    cell.contactNameLabel.text = user.name
    cell.userId = user.documentId
    // Set follow button state
    if followingArray.contains(cell.userId) {
        cell.followButton.isSelected = true
    } else {
        cell.followButton.isSelected = false
    }

    let userImageRef = storage.child("userImages/"+(user.documentId)+"/profile_pic.jpg")
    // Download in memory with a maximum allowed size of 1MB (1 * 1024 * 1024 bytes)
    userImageRef.getData(maxSize: 1 * 1024 * 1024) { (data, error) in
        if let error = error {
            // Uh-oh, an error occurred! Display Default image
            print("Error - unable to download image: \(error)")
            cell.contactImageView.image = UIImage(named: "userProfileGray")
        } else {
            // Data for "locationImages/(locationId).jpg" is returned
            cell.contactImageView.image = UIImage(data: data!)
        }
        SVProgressHUD.dismiss()
    }

    return cell
}

    func getFbFriends() {
    let params = ["fields": "id, first_name, last_name, name, email, picture"]

    let graphRequest = FBSDKGraphRequest(graphPath: "/me/friends", parameters: params)
    let connection = FBSDKGraphRequestConnection()
    connection.add(graphRequest, completionHandler: { (connection, result, error) in
        if error == nil {
            let resultdict = result as! NSDictionary
            print("Result Dict: \(resultdict)")

            let data : NSArray = resultdict.object(forKey: "data") as! NSArray
            for i in 0..<data.count {
                let valueDict : NSDictionary = data[i] as! NSDictionary
                let id = valueDict.object(forKey: "id") as! String
                print("the id value is \(id)")
                let name = valueDict.object(forKey: "name") as! String
                print("the name value is \(name)")

                let picDict = valueDict["picture"] as! NSDictionary
                let picData = picDict["data"] as! NSDictionary
                let picURL = picData.object(forKey: "url") as! String
                print("the url value is \(picURL)")

                self.facebookContacts.append(User(name: name, documentId: id))
            }
            DispatchQueue.main.async {
                self.tableView.reloadData()
            }

            let friends = resultdict.object(forKey: "data") as! NSArray
            print("Found \(friends.count) friends")
        } else {
            print("Error Getting Friends \(String(describing: error))");
        }
    })
    connection.start()
}

1 个答案:

答案 0 :(得分:0)

我认为您不能根据自己的Facebook ID获取用户的UID。看来你有两种可能性:

  1. 通过电子邮件搜索。这应该可以让您精确定位您正在寻找的人。
  2. 实施自定义Facebook身份验证。它需要一些额外的工作,但不是很难做到。