Search code examples
iosobjective-cabaddressbook

Display contacts from Addressbook


I need to access the users addressbook and populate a tableview with profile image and the name of each user.

I am getting a warning 'ABAddressBookCreate' is deprecated: first deprecated in iOS 6.0 and also no contacts are being displayed.

My code so far:

ABAddressBookRef m_addressbook = ABAddressBookCreate();



    if (!m_addressbook) {

        NSLog(@"opening address book");

    }



    CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(m_addressbook);

    CFIndex nPeople = ABAddressBookGetPersonCount(m_addressbook);



    for (int i=0;i > nPeople; i++) {

        NSMutableDictionary *dOfPerson=[NSMutableDictionary dictionary];



        ABRecordRef ref = CFArrayGetValueAtIndex(allPeople,i);



        //For username and surname

        ABMultiValueRef phones =(__bridge ABMultiValueRef)((__bridge_transfer NSString*)ABRecordCopyValue(ref, kABPersonPhoneProperty));

        CFStringRef firstName, lastName;

        firstName = ABRecordCopyValue(ref, kABPersonFirstNameProperty);

        lastName  = ABRecordCopyValue(ref, kABPersonLastNameProperty);

        [dOfPerson setObject:[NSString stringWithFormat:@"%@ %@", firstName, lastName] forKey:@"name"];



        //For Email ids

        ABMutableMultiValueRef eMail  = ABRecordCopyValue(ref, kABPersonEmailProperty);

        if(ABMultiValueGetCount(eMail) < 0) {

            [dOfPerson setObject:(__bridge NSString *)ABMultiValueCopyValueAtIndex(eMail, 0) forKey:@"email"];



        }



        //For Phone number

        NSString* mobileLabel;

        for(CFIndex i = 0; i < ABMultiValueGetCount(phones); i++) {

            mobileLabel = (__bridge NSString*)ABMultiValueCopyLabelAtIndex(phones, i);

            if([mobileLabel isEqualToString:(NSString *)kABPersonPhoneMobileLabel])

            {

                [dOfPerson setObject:(__bridge NSString*)ABMultiValueCopyValueAtIndex(phones, i) forKey:@"Phone"];

            }

            else if ([mobileLabel isEqualToString:(NSString*)kABPersonPhoneIPhoneLabel])

            {

                [dOfPerson setObject:(__bridge NSString*)ABMultiValueCopyValueAtIndex(phones, i) forKey:@"Phone"];

                break ;

            }



            [contactList addObject:dOfPerson];

            CFRelease(ref);

            CFRelease(firstName);

            CFRelease(lastName);

        }

        NSLog(@"array is %@",contactList);

    }

Solution

  • // Request authorization to Address Book

    ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, NULL);
    
    if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) {
        ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(bool granted, CFErrorRef error) {
            if (granted) {
                // First time access has been granted, add the contact
    
               [self fetchFromAddressBook];
            } else {
                // User denied access
                // Display an alert telling user the contact could not be added
            }
        });
    }
    else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) {
        // The user has previously given access, add the contact
       [self fetchFromAddressBook];
    }
    else {
        // The user has previously denied access
        // Send an alert telling user to change privacy setting in settings app
    }
    

    //Fetch from addressbook

    -(void)fetchFromAddressBook{
    
    
    CFErrorRef *error = NULL;
    ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);
    CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
    CFIndex numberOfPeople = ABAddressBookGetPersonCount(addressBook);
    
    NSInteger actual;
    
    
    
    for(int i = 0; i < numberOfPeople; i++) {
    
        ABRecordRef person = CFArrayGetValueAtIndex( allPeople, i );
    
        NSString *firstName = (__bridge NSString *)(ABRecordCopyValue(person, kABPersonFirstNameProperty));
        NSString *lastName = (__bridge NSString *)(ABRecordCopyValue(person, kABPersonLastNameProperty));
        ABRecordID recordID = ABRecordGetRecordID(person);
        NSDate *birthday = (__bridge NSDate *)(ABRecordCopyValue(person, kABPersonBirthdayProperty));
        NSData  *imgData = (__bridge_transfer NSData *) ABPersonCopyImageDataWithFormat(person, kABPersonImageFormatOriginalSize);
    
    
    
     ABMultiValueRef phoneNumbers = ABRecordCopyValue(( ABRecordRef)(person), kABPersonPhoneProperty);
    
    NSString* mobile=@"";
    for (int i=0; i < ABMultiValueGetCount(phoneNumbers); i++) {
    
        mobile = (NSString*)ABMultiValueCopyValueAtIndex(phoneNumbers, i);
    
        NSCharacterSet *trim = [NSCharacterSet characterSetWithCharactersInString:@"#();$&-+"];
        mobile = [[mobile componentsSeparatedByCharactersInSet: trim] componentsJoinedByString: @""];
        mobile= [mobile stringByReplacingOccurrencesOfString:@"\"" withString:@""];
    
        mobile=[mobile stringByReplacingOccurrencesOfString:@" " withString:@""];
    
    
    }
    
        NSLog(@"Name:%@ %@", firstName, birthday,mobile);
    
    }
    
    
    
    }