本文介绍了将联系人从一个源复制到另一个源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试复制本地联系人来源和iCloud联系人来源之间的联系人,但我没有看到任何结果。这段代码执行没有错误,似乎它应该工作,但我没有看到新创建的联系人之后。任何人都看到它的任何问题?

I'm trying to copy contacts between my Local contact source and the iCloud contact source and I'm not seeing any results. This code executes without error and seems like it should work, but I don't see the newly created contacts afterward. Anyone see any issues with it?

ABAddressBookRef addressBook = ABAddressBookCreate();

ABRecordRef abSourceSource = ABAddressBookGetSourceWithRecordID(addressBook, kABSourceTypeLocal);
ABRecordRef abDestinationSource = ABAddressBookGetSourceWithRecordID(addressBook, kABSourceTypeCardDAV);

CFArrayRef sourceContacts = ABAddressBookCopyArrayOfAllPeopleInSource(addressBook, abSourceSource);
CFArrayRef destinationContacts = ABAddressBookCopyArrayOfAllPeopleInSource(addressBook, abDestinationSource);

ABPersonCreatePeopleInSourceWithVCardRepresentation(abDestinationSource, ABPersonCreateVCardRepresentationWithPeople(sourceContacts));
ABPersonCreatePeopleInSourceWithVCardRepresentation(abSourceSource, ABPersonCreateVCardRepresentationWithPeople(destinationContacts)));

ABAddressBookSave(addressBook, NULL);


推荐答案

还有一个更根本的问题 - ABAddressBookGetSourceWithRecordID正确。第二个参数是一个int,它指定通讯录中特定源的记录id。您传递的是一个描述特定源类型的常量。

There is a more fundamental problem - you are not calling ABAddressBookGetSourceWithRecordID properly. The 2nd parameter it takes is an int that specifies the record id of a particular source in your address book. You are passing it a constant that describes the type of a particular source.

传递的常数,kABSourceTypeCardDav始终为4.但是,iCloud源的记录ID

The constant you are passing, kABSourceTypeCardDav is always 4. However, the record id of the iCloud source in a user's address book can be something very different.

您需要做的是枚举所有来源并测试他们的类型,例如:

What you need to do is enumerate all the sources and test their types, like so:

NSArray *allSources = (NSArray*)ABAddressBookCopyArrayOfAllSources(addressBook);

for (int i = 0; i < allSources.count; i++) {
    ABRecordRef src = [allSources objectAtIndex:i];
    NSNumber *stObj = (NSNumber*)ABRecordCopyValue(src, kABSourceTypeProperty);
    ABSourceType st = (ABSourceType)[stObj intValue];

    if (st == kABSourceTypeCardDAV) {
        int recordID = ABRecordGetRecordID(src);
        break;
    }
}

然后你可以使用recordID作为第一个参数函数

Then you could use recordID as the argument to the first function

这篇关于将联系人从一个源复制到另一个源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 15:58