8

I'm trying to convert just ONE iPhone contact to a vCard using the build in methods. The docs say to use:

ABPersonCreateVCardRepresentationWithPeople(CFArrayRef people)

... but my delegate method gives me this:

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person;

I can't figure out how to create a CFArrayRef with just a single ABRecordRef.

The docs pointed me to CFArrayCreate() which confused me even more. I don't know enough C to figure this out on my own. I read in SO that NSArray had something called toll-free bridging and should be interchangeable with CFArrayRefbut didn't quite understand how to use it since the compiler complained when I tried just swapping them.

Julian
  • 8,808
  • 8
  • 51
  • 90

1 Answers1

14

toll-free bridging:

where array is kind of class: NSArray

CFArrayRef arrayRef = (__bridge CFArrayRef)array;
Jesse Gumpo
  • 4,777
  • 1
  • 20
  • 29
  • Thanks. Any special memory considerations that I have to take into account? I'm using ARC. – Julian Jul 14 '12 at 06:55
  • not if you use (__bridge CFArrayRef)array directly in place of where you would use the CFArrayRef – Jesse Gumpo Jul 14 '12 at 06:56
  • 1
    This might work as well: CFArrayRef arrayRef = CFArrayCreate(NULL, (const void**)person, sizeof(person)/sizeof(ABRecordRef), &kCFTypeArrayCallBacks); – Jesse Gumpo Jul 14 '12 at 07:01
  • @Julian Would you mind posting a full solution? I need to do the same thing and the bridging syntax has me all confused. Thanks! – Allen Aug 03 '12 at 21:13
  • @Joshua I'm not exactly sure what you mean by a "full solution". Creating a CFArrayRef is just that one liner the above solution states. If you meant for me to post the full code to generate a vCard, I can't as that code is under NDA. I can offer you a link another SO user posted which does exactly the same thing and works perfectly. http://altoshstock.blogspot.com/2010/11/iphone-os-generate-vcard.html – Julian Aug 03 '12 at 21:54
  • @Julian - Thanks and I appreciate it. I was mainly looking for help on how to add a single ABRecordRef to an NSArray and then cast it as a CFArrayRef. I'll check out that link you posted. – Allen Aug 03 '12 at 23:50
  • 1
    @Joshua you'll be happy to know that what you are looking for is another one-liner. Check this other answer for the explanation: http://stackoverflow.com/questions/6071256/how-can-i-add-a-abrecordref-to-a-nsmutablearray-in-iphone – Julian Aug 04 '12 at 15:21