Phone 与 Firebase 用户的联系

Phone Contacts With Firebase User

我正在使用 Firebase 创建一个聊天应用程序,我可以从 firebase 数据库中获取用户。我想要的是我的用户列表必须只显示联系人保存在我的 phone 中的用户,而不是显示所有联系人。 我无法弄清楚我应该如何开始。 下面是获取用户的方法

func fetchUser(){
    Database.database().reference().child("Users").observe(.childAdded, with: { (snapshot) in

        if let dictonary = snapshot.value as? [String: AnyObject]{
            let user = Users()
            user.id = snapshot.key
         user.setValuesForKeys(dictonary)
            print(user.phoneNumber!,user.userName!,user.UserId!)


            self.users.append(user)

            DispatchQueue.main.async {
                self.tableView.reloadData()
            }
        }


    }, withCancel: nil)
}

下面是从 phone

获取联系人的函数
private func fetchContacts() {
    print("Attempting to fetch contacts today..")

    let store = CNContactStore()

    store.requestAccess(for: .contacts) { (granted, err) in
        if let err = err {
            print("Failed to request access:", err)
            return
        }
        if granted {
            print("Access granted")

            let keys = [CNContactGivenNameKey, CNContactFamilyNameKey, CNContactPhoneNumbersKey]
            let request = CNContactFetchRequest(keysToFetch: keys as [CNKeyDescriptor])

            do {

                try store.enumerateContacts(with: request, usingBlock: { (contact, stopPointerIfYouWantToStopEnumerating) in


                    let ph = (contact.phoneNumbers[0].value ).value(forKey: "digits") as! String
                    self.contacts.append(Contact(givenName: contact.givenName, familyName: contact.familyName, phoneNumbers: ph))

                })

            } catch let err {
                print("Failed to enumerate contacts:", err)
            }

        } else {
            print("Access denied..")
        }
    }
}

现在我该怎么做才能让我的 tableview 只显示保存了联系人的用户?

在上面的代码中稍作调整,我终于让它成为可能

            func fetchUser(){
        Database.database().reference().child("Users").observe(.childAdded, with: { (snapshot) in

            if let dictonary = snapshot.value as? [String: AnyObject]{
                let user = Users()
                user.id = snapshot.key
             user.setValuesForKeys(dictonary)
                ///////
                let store = CNContactStore()

                store.requestAccess(for: .contacts) { (granted, err) in
                    if let err = err {
                        print("Failed to request access:", err)
                        return
                    }
                    if granted {
                        print("Access granted")

                        let keys = [CNContactGivenNameKey, CNContactFamilyNameKey, CNContactPhoneNumbersKey]
                        let request = CNContactFetchRequest(keysToFetch: keys as [CNKeyDescriptor])

                        do {

                            try store.enumerateContacts(with: request, usingBlock: { (contact, stopPointerIfYouWantToStopEnumerating) in


                                let ph = (contact.phoneNumbers[0].value ).value(forKey: "digits") as! String
                                self.contacts.append(Contact(givenName: contact.givenName, familyName: contact.familyName, phoneNumbers: ph))


                                if user.phoneNumber == ph {
                                    print("Similar Contact Found")
                                    self.users.append(user)
                                    DispatchQueue.main.async {
                                        self.tableView.reloadData()
                                    }
                                } else {

                                    print("Contacts Not Compared")

                                }

                            })

                        } catch let err {
                            print("Failed to enumerate contacts:", err)
                        }

                    } else {
                        print("Access denied..")
                    }
                }



            }


        }, withCancel: nil)
    }