在本地创建的CNContact不会同步到iCloud帐户

时间:2016-09-01 19:56:23

标签: iphone ios9 icloud contacts

最近,我们的一些用户抱怨联系人没有同步到他们的iCloud帐户。它在iOS 8中工作,并在iOS 9的其中一个更新中神秘地停止。随着iOS 10的临近,我认为它可能与AddressBook.frameworkContacts.framework的弃用有关。但是,即使转移到新的Contacts.framework,它也无法解决问题。

设备控制台上没有错误日志,在设备上创建/更新联系人时,两个框架都不会产生任何错误。

设备上的联系人可见且无法与iCloud和连接到iCloud帐户的其他设备同步。

2 个答案:

答案 0 :(得分:3)

经过大量调试后,我能够将问题隔离到imageData属性。已填充imageData的联系人未同步,少数没有图像的联系人同步。这导致我查看imageData的代码。事实证明我一直在使用UIImagePNGRepresentationUIImage转换为NSData imageData。转到UIImageJPEGRepresentation修复了问题。这一天被保存,iCloud帐户被同步。

感谢Apple没有记录此更改。 (可能是图像尺寸,即使没有记录)

答案 1 :(得分:0)

CNContact(s) created locally are not synced to Third party application.

I have crated contacts in iphone contacts page.Those contacts are not synching inside my application.I have used below method to fetch contacs from my contacts framework.

There are no error logs on console of the device and neither of the frameworks generate any errors when contacts are being created/updated on the device.

Contacts are visible and available on the device just not syncing to my app.I have given permission to my application to access my contacts.

@available(iOS 10.0, *)
func retrieveContacts(_ completion: (_ success: Bool, _ contacts: [ContactEntry]?) -> Void) {
    var contacts = [ContactEntry]()
    do {
        let contactsFetchRequest = CNContactFetchRequest(keysToFetch: [CNContactGivenNameKey as CNKeyDescriptor, CNContactFamilyNameKey as CNKeyDescriptor, CNContactImageDataKey as CNKeyDescriptor, CNContactImageDataAvailableKey as CNKeyDescriptor, CNContactPhoneNumbersKey as CNKeyDescriptor, CNContactEmailAddressesKey as CNKeyDescriptor])
        try contactStore.enumerateContacts(with: contactsFetchRequest, usingBlock: { (cnContact, error) in
            if let contact = ContactEntry(cnContact: cnContact) {
                contacts.append(contact)
                print(contact,contacts.count)
            }
        })
        completion(true, contacts)
    } catch {
        completion(false, nil)
    }

    let keysToFetch = [CNContactFormatter.descriptorForRequiredKeys(for: .fullName),CNContactPhoneNumbersKey] as [Any]
    let fetchRequest = CNContactFetchRequest( keysToFetch: keysToFetch as! [CNKeyDescriptor])
    var contacts1 = [CNContact]()

    if #available(iOS 10.0, *) {
        fetchRequest.mutableObjects = false
    } else {
        // Fallback on earlier versions
    }
    fetchRequest.unifyResults = true
    fetchRequest.sortOrder = .userDefault

    let contactStoreID = CNContactStore().defaultContainerIdentifier()
    print("\(contactStoreID)")


    do {

        try CNContactStore().enumerateContacts(with: fetchRequest) { (contact, stop) -> Void in

                contacts1.append(contact)
            print(contact)
        }
    } catch let e as NSError {
        print(e.localizedDescription)
    }