Over a million developers have joined DZone.

Contacts UI, Contacts Framework, and Create VCard(VCF) in Objective-C

In this post, we will go over how to use the two new frameworks Apple introduced and export VCard.

· Mobile Zone


Before AddressBookUI.framework and Addressbook.framework becomes deprecated, Apple introduced ContactUI.framework and Contact.framework with enhancements over AddressBookUI.framework and Addressbook.framework. In this blog, we will talk about how to use these two new frameworks and export VCard. Let's start picking a contact from phone contacts and access basic information for that person.

Step 1. Create new Xcode project name “ContactDemo” and import Contacts.framework and ContactsUI.framework as shown in the picture below:

Image title

Step 2. In project add Button,  ImageView , and 3 labels as shown in the picture below:

Image title

Step 3. Create outlets of button action,  imageview, and labels in respective view controller as:

@property (weak, nonatomic) IBOutlet UIImageView *personImage;
@property (weak, nonatomic) IBOutlet UILabel *personName;
@property (weak, nonatomic) IBOutlet UILabel *emailId;
@property (weak, nonatomic) IBOutlet UILabel *phoneNo;

- (IBAction)selectAction:(id)sender;

Step 4. Add delegate  “CNContactPickerDelegate"  to  viewcontroller.

Step 5. Add delegate method:

- (void) contactPicker:(CNContactPickerViewController *)picker
didSelectContact:(CNContact *)contact {
[self getContactDetails:contact];

This delegate method will return contact in the form of  CNContact  object which will be further processed in local method:

-(void)getContactDetails:(CNContact *)contactObject {

NSLog(@"NAME PREFIX :: %@",contactObject.namePrefix);
NSLog(@"NAME SUFFIX :: %@",contactObject.nameSuffix);
NSLog(@"FAMILY NAME :: %@",contactObject.familyName);
NSLog(@"GIVEN NAME :: %@",contactObject.givenName);
NSLog(@"MIDDLE NAME :: %@",contactObject.middleName);

NSString * fullName = [NSString stringWithFormat:@"%@ %@",contactObject.givenName,contactObject.familyName];
[self.personName setText:fullName];

if(contactObject.imageData) {
NSData * imageData = (NSData *)contactObject.imageData;
UIImage * contactImage = [[UIImage alloc] initWithData:imageData];
[self.personImage setImage:contactImage];

NSString * phone = @"";
NSString * userPHONE_NO = @"";
for(CNLabeledValue * phonelabel in contactObject.phoneNumbers) {
CNPhoneNumber * phoneNo = phonelabel.value;
phone = [phoneNo stringValue];
if (phone) {
userPHONE_NO = phone;

NSString * email = @"";
NSString * userEMAIL_ID = @"";
for(CNLabeledValue * emaillabel in contactObject.emailAddresses) {
email = emaillabel.value;
if (email) {
userEMAIL_ID = email;

NSLog(@"PHONE NO :: %@",userPHONE_NO);
NSLog(@"EMAIL ID :: %@",userEMAIL_ID);
[self.emailId setText:userEMAIL_ID];
[self.phoneNo setText:userPHONE_NO];

Step 6. Create “CNContactPickerViewController” class object and register its delegate in button IBAction method :

- (IBAction) selectAction:(id)sender {
CNContactPickerViewController *contactPicker = [CNContactPickerViewController new];
contactPicker.delegate = self;
[self presentViewController:contactPicker animated:YES completion:nil];

 “[self presentViewController:contactPicker animated:YES completion:nil];”  will present view of contact list.

Step 7. Run project.

A . Main View

Image title

B. On Tapping “Select Contact” button  CNContactPickerViewController will open as shown in the picture below:

Image title

C. Pick one contact and view will dismiss and you will get details of that contact as shown in the picture below:

Image title

Earlier we wrote permission code to access contacts but now it implicitly grants permission for accessing contacts. With this framework, we can also generate  VCard(VCF)  and share among other platforms. Here are the steps to create VCard:

Step 1. Pick the contact from  CNContactPickerViewController  and you will get  CNContact Object  in delegate, as mention above.

Step 2. Save contact in document directory. As data is stored in NSData form so to convert contact to NSData.

Use  “CNContactVCardSerialization”  class that represents VCard in NSData format.

- (NSString *) saveContactToDocumentDirectory:(CNContact *)contact {

NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory,     NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString * VCardPath = [documentsDirectory stringByAppendingString:@"/VCard.vcf"];

NSArray *array = [[NSArray alloc] initWithObjects:contact, nil];

NSError *error;
NSData *data = [CNContactVCardSerialization dataWithContacts:array error:&error];
[data writeToFile:VCardPath atomically:YES];

return VCardPath;

CNContactVCardSerialization  class method  “dataWithContacts:error:” takes array of contact  objects(CNContact class Object) .

 “saveContactToDocumentDirectory”  method will return the file path of Vcard. With File path you can export contact anywhere you want.

If you are still using iOS<9 Here is the sample code VCard Using AddressBook.

ios ,objective-c

Published at DZone with permission of Abhishek Thapliyal. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}