将联系人从一个源复制到另一个源

发布于 2024-12-11 19:24:39 字数 903 浏览 4 评论 0原文

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

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);

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);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

独﹏钓一江月 2024-12-18 19:24:39

还有一个更根本的问题 - 您没有正确调用 ABAddressBookGetSourceWithRecordID。它采用的第二个参数是一个 int,指定地址簿中特定源的记录 ID。您向其传递一个描述特定源类型的常量。

您传递的常量 kABSourceTypeCardDav 始终为 4。但是,用户地址簿中 iCloud 源的记录 ID 可能非常不同。

您需要做的是枚举所有源并测试它们的类型,如下所示:

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 作为第一个函数的参数

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.

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;
    }
}

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

幸福不弃 2024-12-18 19:24:39

我认为您忘记使用 ABAddressBookAddRecord 添加记录。这是我的工作示例:

ABAddressBookRef addressBook = ABAddressBookCreate();
ABRecordRef abSource = ABAddressBookGetSourceWithRecordID(addressBook, kABSourceTypeLocal);
NSURL *theURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"some.vcf"];
NSData *vCardData = [NSData dataWithContentsOfURL:theURL];
NSLog(@"data %@", vCardData);
NSArray *createdPeople = (__bridge_transfer NSArray*)ABPersonCreatePeopleInSourceWithVCardRepresentation(abSource, (__bridge CFDataRef)vCardData);
NSLog(@"createdPeople %@", createdPeople);
CFErrorRef error = NULL;
bool ok;
for (id person in createdPeople) {
    error = NULL;
    ok = ABAddressBookAddRecord(addressBook, (__bridge ABRecordRef)person, &error);
    if (!ok) {
        NSLog(@"add err %@", error);  
        break;
    } 
}
if (ok) {
    error = NULL;
    BOOL isSaved = ABAddressBookSave(addressBook, &error);
    if (isSaved) {
        NSLog(@"saved..");
    }
    if (error != NULL) {
        NSLog(@"ABAddressBookSave %@", error);
    } 
}
CFRelease(addressBook);    

I think you forgot to add the records with ABAddressBookAddRecord. Here is my working example:

ABAddressBookRef addressBook = ABAddressBookCreate();
ABRecordRef abSource = ABAddressBookGetSourceWithRecordID(addressBook, kABSourceTypeLocal);
NSURL *theURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"some.vcf"];
NSData *vCardData = [NSData dataWithContentsOfURL:theURL];
NSLog(@"data %@", vCardData);
NSArray *createdPeople = (__bridge_transfer NSArray*)ABPersonCreatePeopleInSourceWithVCardRepresentation(abSource, (__bridge CFDataRef)vCardData);
NSLog(@"createdPeople %@", createdPeople);
CFErrorRef error = NULL;
bool ok;
for (id person in createdPeople) {
    error = NULL;
    ok = ABAddressBookAddRecord(addressBook, (__bridge ABRecordRef)person, &error);
    if (!ok) {
        NSLog(@"add err %@", error);  
        break;
    } 
}
if (ok) {
    error = NULL;
    BOOL isSaved = ABAddressBookSave(addressBook, &error);
    if (isSaved) {
        NSLog(@"saved..");
    }
    if (error != NULL) {
        NSLog(@"ABAddressBookSave %@", error);
    } 
}
CFRelease(addressBook);    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文