ios - 当应用程序在ios swift中处于活动状态时,如何为联系人进行后台同步?
问题描述
我能够在应用程序中同步电话联系人,但我需要电话联系人应该在后台同步到应用程序。
像下面的android
我可以使用以下代码正常同步:
func getLocalContacts(completion:@escaping contacts) {
let contacts = CNContact()
let contactStore = CNContactStore()
let keys = [CNContactGivenNameKey,CNContactFamilyNameKey, CNContactPhoneNumbersKey]//[CNContactFormatter.descriptorForRequiredKeys(for: .fullName),CNContactPhoneNumbersKey] as [Any]
let request = CNContactFetchRequest(keysToFetch: keys as! [CNKeyDescriptor])
do {
try contactStore.enumerateContacts(with: request) {(contact, stop) in
completion(contact)
}
} catch {
print(error.localizedDescription)
completion(contacts)
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return contacts.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: ContactsTableViewCell = tableView.dequeueReusableCell(withIdentifier: "ContactsTableViewCell") as! ContactsTableViewCell
cell.nameLbl.text = contacts[indexPath.row].givenName + " " + contacts[indexPath.row].familyName
cell.empRoleLbl.text = contacts[indexPath.row].phoneNumbers.first?.value.stringValue
print("table phnums \(contacts[indexPath.row].phoneNumbers.first?.value.stringValue)")
cell.inviteButn.addTarget(self, action: #selector(connected(sender:)), for: .touchUpInside)
return cell
}
或如何使用 GCD 同步联系人 请帮我在应用处于活动状态时同步联系人背景。
解决方案
推荐阅读
- java-8 - DataFlow 管道卡在初始化 tempLocation 上?
- c++ - 我是否应该期望 GMP 的 mpf_class 比原始数据类型 double 慢得多?
- c# - 了解编译器优化
- excel - 有没有办法让 VBA excel 在文件中找到一个单词/字符串并返回它旁边的单元格中的值?
- python - 如何接受 ModelForm 中的数据并在同一网页中显示来自 DB 的数据
- batch-file - 只需在文件中打印 13 到 16 个字符(数字/字母)并将其存储为变量以供以后使用批处理文件调用
- python - Python:初始化模块后,不再可以导入函数
- python - 来自 YAML 的 Numpy 数组
- laravel - 如何在 Vuejs 中动态管理页面标题?
- html - Bootstrap 列包装在移动设备上,应该完全适合内联