首页 > 解决方案 > 当应用程序在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 同步联系人 请帮我在应用处于活动状态时同步联系人背景。

标签: iosswiftsynchronizationcontacts

解决方案


推荐阅读