huddie96 huddie96 -3 years ago 76
Objective-C Question

Getting/Generating vCard (.vtf file) from Address Book ID

Im looking for a way to generate/get an electronic business card (vCard) from contacts using an Address Book ID.

I have been able to do this using the users name using this code

ABAddressBookRef ab = ABAddressBookCreateWithOptions(NULL, nil);

NSMutableDictionary *object = [_Contacts objectAtIndex:indexPath.row];

NSString *name = [object objectForKey:@"name"];

CFArrayRef contact = ABAddressBookCopyPeopleWithName(ab, (__bridge CFStringRef)(name));

CFDataRef vcard = (CFDataRef)ABPersonCreateVCardRepresentationWithPeople(contact);


The only problem is that when I have multiple contacts with the same name (not sure why this happens but I do) Instead of getting a single (vCard) I get multiple vCards and when I send the vCard's instead of saying "Users name" it says electronic business card.

I was hoping there a way to be more exact in which card I'm looking for using the address book Id. Is there anyway to do this?

EDIT 1
Im getting an error when trying this.

int32_t ID = [[selectedObject objectForKey:@"ABID"]intValue];
CFArrayRef contact = ABAddressBookGetPersonWithRecordID(ab,ID);


CFDataRef vcard = (CFDataRef)ABPersonCreateVCardRepresentationWithPeople(contact);


Error message


[__NSCFType objectEnumerator]: unrecognized selector sent to instance
0x144e51280 2015-05-18 09:13:34.511 theBoard[6854:240618] ***
Terminating app due to uncaught exception
'NSInvalidArgumentException', reason: '-[__NSCFType objectEnumerator]:
unrecognized selector sent to instance 0x144e51280'

Answer Source

what ewan meant:

int32_t ID = [[selectedObject objectForKey:@"ABID"]intValue];
ABRecordRef contact = ABAddressBookGetPersonWithRecordID(ab,ID);
CFArrayRef array = CFArrayCreate(nil, &contact, 1, nil);
CFDataRef vcard = (CFDataRef)ABPersonCreateVCardRepresentationWithPeople(array);

just casting a value doesn't make an array. you have to create one

just in case I didn't get it: Creating an immutable CFArray object - Apple sample doesn't work

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download