Showing posts with label Addressbook. Show all posts
Showing posts with label Addressbook. Show all posts

Wednesday, 25 May 2016

Getting Contacts from Address Book for iOS App

1. Add the AddressBookUI.framework into Project

2. Import the header file
  #import <AddressBook/AddressBook.h>

3. Add these code for where u want

/* Getting Contact Details (Single Phone Number - Name, Phone Number, Email */
-(void)getContacts
{
    ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, NULL);
    
    if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined)
    {
        ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(bool granted, CFErrorRef error)
        {
        });
    }
    else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized)
    {
        CFErrorRef *error = NULL;
        ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);
        CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
        CFIndex numberOfPeople = ABAddressBookGetPersonCount(addressBook);
        
        for(int i = 0; i < numberOfPeople; i++)
        {
            ABRecordRef person = CFArrayGetValueAtIndex( allPeople, i );
            
            ABMultiValueRef email = ABRecordCopyValue(person, kABPersonEmailProperty);
            ABMultiValueRef phoneNumbers = ABRecordCopyValue(person, kABPersonPhoneProperty);
            NSNumber *recordId = [NSNumber numberWithInteger:ABRecordGetRecordID(person)];
            
            NSString *firstName = (__bridge NSString *)(ABRecordCopyValue(person, kABPersonFirstNameProperty));
            NSString *phoneNumber = (__bridge_transfer NSString *) ABMultiValueCopyValueAtIndex(phoneNumbers, 0);
            NSString *EmailId = (__bridge_transfer NSString *) ABMultiValueCopyValueAtIndex(email, 0);
            NSString *GroupName=(__bridge_transfer NSString *) ABMultiValueCopyLabelAtIndex(phoneNumbers, 0);
            GroupName = [GroupName stringByReplacingOccurrencesOfString:@"_$!<" withString:@""];
            GroupName = [GroupName stringByReplacingOccurrencesOfString:@">!$_" withString:@""];
            NSLog(@"Name: %@ | Phone Number(%@): %@ | Email: %@ | Record ID: %@", firstName, GroupName, phoneNumber, EmailId, recordId);
        }
    }
}

/* Getting Contact Details (Multiple Phone Numbers) - Name, Phone Number */
-(void)getContacts
{
    ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, NULL);
    
    if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined)
    {
        ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(bool granted, CFErrorRef error)
        {
        });
    }
    else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized)
    {
        CFErrorRef *error = NULL;
        ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);
        CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
        CFIndex numberOfPeople = ABAddressBookGetPersonCount(addressBook);
        
        for(int i = 0; i < numberOfPeople; i++)
        {
            ABRecordRef person = CFArrayGetValueAtIndex( allPeople, i );
            
            ABMultiValueRef email = ABRecordCopyValue(person, kABPersonEmailProperty);
            ABMultiValueRef phoneNumbers = ABRecordCopyValue(person, kABPersonPhoneProperty);
            NSNumber *recordId = [NSNumber numberWithInteger:ABRecordGetRecordID(person)];
        
            for (CFIndex i = 0; i < ABMultiValueGetCount(phoneNumbers); i++)
            {
                NSString *first_Name = (__bridge NSString *)(ABRecordCopyValue(person, kABPersonFirstNameProperty));
                NSString *last_Name = (__bridge NSString *)(ABRecordCopyValue(person, kABPersonLastNameProperty));
                NSString *str_Name = [NSString stringWithFormat:@"%@ %@", first_Name, last_Name];
                NSString *phone_Number = (__bridge_transfer NSString *) ABMultiValueCopyValueAtIndex(phoneNumbers, i);
                NSString *Group_Name=(__bridge_transfer NSString *) ABMultiValueCopyLabelAtIndex(phoneNumbers, i);
                Group_Name = [Group_Name stringByReplacingOccurrencesOfString:@"_$!<" withString:@""];
                Group_Name = [Group_Name stringByReplacingOccurrencesOfString:@">!$_" withString:@""];
                
                NSLog(@"Name: %@ | Phone Number(%@): %@ | Record ID: %@", str_Name, GroupName, phone_Number, recordId);
            }
        }
    }

}