Copy contacts from one source to another

I am trying to copy contacts between my local contact source and iCloud contact source, and I do not see any results. This code is executed without errors and it seems that it should work, but after that I do not see the newly created contacts. Does anyone see any problems 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); 
+2
source share
2 answers

There is a more fundamental problem: you are not calling ABAddressBookGetSourceWithRecordID properly. The second parameter that it takes is an int, which indicates the identifier of a particular source entry in your address book. You pass it a constant describing the type of a particular source.

The constant you pass in is kABSourceTypeCardDav is always 4. However, the identifier of an iCloud source record in a user's address book can be something completely different.

What you need to do is list all the sources and test their types, for example:

 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 can use recordID as an argument for the first function

+4
source

I think you forgot to add entries using 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); 
+2
source

Source: https://habr.com/ru/post/1387346/


All Articles