在物理设备-iOS上不能获取联系人

时间:2018-05-13 01:18:35

标签: ios swift firebase firebase-realtime-database testflight

我目前正在尝试使用TestFlight测试我的iOS应用,并且遇到了一些提取联系人的问题。我的应用程序应该检索联系人簿中的所有联系人,然后在检查firebase后将其显示在表格视图中,以确保联系人尚未被邀请加入应用程序。它在具有10个联系人的模拟器上工作正常但在我的手机上没有100个以上的联系人。甚至没有联系人出现。我已经检查过我也启用了联系人。不确定问题是什么。我附上了以下代码:

override func viewDidLoad() {
    super.viewDidLoad()

    contactStore.requestAccess(for: .contacts) { (success, error) in
        if success{
            print("Contact Authorization Successful")
        }
    }
    fetchContacts()
    tableView.delegate = self
    tableView.dataSource = self
}
func fetchContacts(){
    contacts = [ContactStruct]()
    let key = [CNContactGivenNameKey, CNContactFamilyNameKey, CNContactPhoneNumbersKey, CNContactImageDataKey, CNContactThumbnailImageDataKey] as [CNKeyDescriptor]
    let request = CNContactFetchRequest(keysToFetch: key)
    try! contactStore.enumerateContacts(with: request) { (contact, stoppingPointer) in
        let name = contact.givenName
        let familyName = contact.familyName
        let fullName = contact.givenName + contact.familyName
        let number = (contact.phoneNumbers[0].value ).value(forKey: "digits") as! String
        let contactImage = contact.imageData


        let phone = (contact.phoneNumbers[0].value ).value(forKey: "digits") as! String

            self.phoneNumbersArray.append(phone)


        let contactToAppend = ContactStruct(givenName: name, familyName: familyName, fullName: fullName, phoneNumber: number, contactImage: contactImage)

        self.contacts.append(contactToAppend)
        self.tableView.reloadData()
    }
    getAllUsers()
}


func getAllUsers(){
    self.sectionData = [0: self.contactsOnApp as Array<AnyObject>, 1: self.usersInSchool as Array<AnyObject>, 2: self.contacts as Array<AnyObject>]
    ref.child("users").child(uid!).observeSingleEvent(of: .value, with: { (snapshot) in
        let user = User(snapshot: snapshot)
        let currentUserSchool = user.highSchool!

        for child in user.invitesFormatted {
            let checkInvites = child
                if let alreadyInvited = self.contacts.index(where: {$0.phoneNumber == checkInvites}){
                    self.contacts.remove(at: alreadyInvited)
                    self.sectionData = [0: self.contactsOnApp as Array<AnyObject>, 1: self.usersInSchool as Array<AnyObject>, 2: self.contacts as Array<AnyObject>]
                    self.tableView.reloadData()
                }
        }

        ref.child("schools").child(currentUserSchool).child("members").observeSingleEvent(of: .value, with: { (snap) in

            for child in snap.children{

                let child = child as? DataSnapshot
                if let otherUsers = child?.key {
                    ref.child("users").child(otherUsers).observeSingleEvent(of: .value, with: { (snappy) in
                        let personInSchool = User(snapshot: snappy)

                        if personInSchool.uid != uid! && self.phoneNumbersArray.contains(personInSchool.phoneNumber) {
                            //If contacts does have user
                            self.contactsOnApp.append(personInSchool)

                                if let itemToRemoveIndex = self.contacts.index(where: {$0.phoneNumber == personInSchool.phoneNumber}) {
                                        self.contacts.remove(at: itemToRemoveIndex)
                            }   
                        }else if personInSchool.uid != uid! {
                            //If contacts doesn't have user
                            self.usersInSchool.append(personInSchool)   
                        }
                        self.sectionData = [0: self.contactsOnApp as Array<AnyObject>, 1: self.usersInSchool as Array<AnyObject>, 2: self.contacts as Array<AnyObject>]
                        self.tableView.reloadData()
                    })
                }
            }
        })
    })
}

非常感谢任何帮助。谢谢!

0 个答案:

没有答案