Screen Shot 2016-05-13 at 2.50.41 PM

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

From iOS 9+ version 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 contact from phone contacts and access basic information of that person.Screen Shot 2016-05-13 at 2.50.41 PM

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

Screen Shot 2016-05-13 at 2.41.17 PM

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

Screen Shot 2016-05-13 at 3.23.29 AM

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

Screen Shot 2016-05-13 at 2.38.04 AM

 

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

Screen Shot 2016-05-13 at 2.39.11 AM

 

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

Screen Shot 2016-05-13 at 2.39.25 AM

Earlier we have write 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 is the steps to create VCard.

Step 1. Pick 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

Add Applozic chat framework project
Applozic Android/iOS Chat SDK Review by FinancesOnline

  • Sakshi Chavan

    What if I want to export all contacts and create one .vcf file for it !