How to create CFArrayRef
Asked Answered
S

1

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.

Smoothie answered 14/7, 2012 at 6:31 Comment(0)
C
14

toll-free bridging:

where array is kind of class: NSArray

CFArrayRef arrayRef = (__bridge CFArrayRef)array;
Cavill answered 14/7, 2012 at 6:39 Comment(7)
Thanks. Any special memory considerations that I have to take into account? I'm using ARC.Smoothie
not if you use (__bridge CFArrayRef)array directly in place of where you would use the CFArrayRefCavill
This might work as well: CFArrayRef arrayRef = CFArrayCreate(NULL, (const void**)person, sizeof(person)/sizeof(ABRecordRef), &kCFTypeArrayCallBacks);Cavill
@Smoothie Would you mind posting a full solution? I need to do the same thing and the bridging syntax has me all confused. Thanks!Luge
@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. altoshstock.blogspot.com/2010/11/iphone-os-generate-vcard.htmlSmoothie
@Smoothie - 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.Luge
@Joshua you'll be happy to know that what you are looking for is another one-liner. Check this other answer for the explanation: #6071756Smoothie

© 2022 - 2024 — McMap. All rights reserved.