我目前正在尝试使用 TestFlight
测试我的 iOS 应用程序,但在获取联系人时遇到了一些问题。我的应用程序应该检索通讯录中的所有联系人,然后在检查 firebase 以确保联系人尚未被邀请到应用程序后将它们显示在表格 View 中。它在有 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() 将永远无法工作。
在真实设备上全新安装该应用。如果您请求正确使用用户联系人的权限,那么这应该会触发应用程序询问使用情况。确保在弹出消息上单击"is"。您还可以检查“设置”应用程序下的“应用程序设置”,然后重新启用该应用程序以允许访问联系人。
关于ios - 未在物理设备上获取联系人 - iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50311928/