簡體   English   中英

聯系人無法在物理設備上獲取-iOS

[英]Contacts Not Fetching On Physical Device-iOS

我目前正在嘗試使用TestFlight測試我的iOS應用,並且在獲取聯系人時TestFlight了一些問題。 我的應用程序應該檢索通訊錄中的所有聯系人,然后在檢查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()
                    })
                }
            }
        })
    })
}

任何幫助將不勝感激。 謝謝!

僅當設備已授予應用訪問權限時,您才能提取聯系人。 我的猜測是您沒有授予應用訪問權限,因此您的fetchContacts()將永遠無法工作。

在真實設備上重新安裝該應用程序。 如果您要求權限以正確使用用戶聯系人,則這將觸發應用程序要求使用。 確保在彈出消息上單擊“是”。 您還可以在“設置”應用程序下檢查“應用程序設置”,然后重新啟用該應用程序以允許訪問聯系人。

設置應用下的應用設置

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM