【发布时间】:2023-04-06 17:38:01
【问题描述】:
我目前正在尝试使用 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()
})
}
}
})
})
}
任何帮助将不胜感激。谢谢!
【问题讨论】:
-
您需要将调用移动到
fetchContacts以在调用requestAccess的完成处理程序中并且仅当success是true时。 -
@rmaddy 好的,我试试,但是为什么它在模拟器上可以运行,而在设备上却不行?
-
你也有很多电话给
reloadData。为什么不在所有数据完全加载后调用一次呢? -
@rmaddy 主要是因为我不确定数据何时/何地完全加载,因为 firebase 函数是异步的。有没有要加的地方?另外,你知道为什么它仍然可以在模拟器上运行吗?
-
您可能在某个时候授予了访问权限。在模拟器或真机上删除并重新测试。做一些调试,看看哪里没有按预期工作。
标签: ios swift firebase firebase-realtime-database testflight