0

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'

huddie96
  • 1,240
  • 2
  • 13
  • 26

2 Answers2

1

Are you looking for ABAddressBookGetPersonWithRecordID? You can then make a CFArrayRef with just the one entry, and pass that to ABPersonCreateVCardRepresentationWithPeople instead.

Ewan Mellor
  • 6,747
  • 1
  • 24
  • 39
1

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

Community
  • 1
  • 1
Daij-Djan
  • 49,552
  • 17
  • 113
  • 135
  • CFArrayRef array = CFBridgingRelease(@[(__bridge id)contact]); is giving me an error. and you meant to place "array" inside ABPersonCreateVCardRepresentationWithPeople not "contact" correct? – huddie96 May 18 '15 at 13:42
  • edited, check and fix as needed but this is basically it - the other way used cocoa.. this looks nicer - less casting ;) – Daij-Djan May 18 '15 at 13:46