user2951348
user2951348

Reputation: 113

Fetch mobile number programmatically

I am developing an app in iOS. I can retrieve the first and last name of person but what i want is how can I retrieve the mobile number information. I already have this code to fetch first and last name.

CFErrorRef error = NULL;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error);

if (addressBook != nil) {
    NSLog(@"Succesful.");


    NSArray *allContacts = (__bridge_transfer NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);


    NSUInteger i = 0; for (i = 0; i < [allContacts count]; i++)
    {

        ABRecordRef contactPerson = (__bridge ABRecordRef)allContacts[i];


        firstName = (__bridge_transfer NSString *)ABRecordCopyValue(contactPerson,kABPersonFirstNameProperty);
        lastName = (__bridge_transfer NSString *)ABRecordCopyValue(contactPerson, kABPersonLastNameProperty);


        fullName = [NSString stringWithFormat:@"%@ %@", firstName, lastName];


} else {

    NSLog(@"Error Reading Address Book");
}

Upvotes: 0

Views: 477

Answers (3)

Bhumeshwer katre
Bhumeshwer katre

Reputation: 4671

Try this

  ABAddressBookRef addressBook = ABAddressBookCreate(); 

  CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);

  CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);
  for(int i = 0; i < nPeople; i++) 
  { 
      //  Getting the person record ... 
       ABRecordRef person = CFArrayGetValueAtIndex(allPeople, i); 
       ABMultiValueRef phones = ABRecordCopyValue(person, kABPersonPhoneProperty);

       for (CFIndex i=0; i < ABMultiValueGetCount(phones); i++) 
       {
            NSString* phoneLabel = (__bridge_transfer  NSString*)ABMultiValueCopyLabelAtIndex(phones, i);
            NSString* phoneNumber = (__bridge_transfer  NSString*) ABMultiValueCopyValueAtIndex(phones, i);

       }
       CFRelease(phones);
  }
  CFRelease(allPeople);
  CFRelease(addressBook);

Upvotes: 0

Grzegorz Krukowski
Grzegorz Krukowski

Reputation: 19872

You can get all phone entries using the code below. I also specified few types of phone types to give you an idea how to deal with that.

ABMultiValueRef phoneNumbers = ABRecordCopyValue(contactPerson, kABPersonPhoneProperty);

for (CFIndex i=0; i < ABMultiValueGetCount(phoneNumbers); i++) 
{
    NSString* phoneLabel = (NSString*)ABMultiValueCopyLabelAtIndex(phoneNumbers, i);
    NSString* phoneNumber = ABMultiValueCopyValueAtIndex(phoneNumbers, i);

    if([phoneLabel isEqualToString:(NSString *)kABPersonPhoneMobileLabel])
    {

    }
    else if([phoneLabel isEqualToString:(NSString *)kABPersonPhoneIPhoneLabel])
    {

    }

    CFRelease(phoneNumber);
    CFRelease(phoneLabel);
}

CFRelease(phoneNumbers);

Upvotes: 3

Pradhyuman sinh
Pradhyuman sinh

Reputation: 3928

You can fetch phone numbers using below code

 ABMultiValueRef phoneNumbers = ABRecordCopyValue(person, kABPersonPhoneProperty);

Upvotes: 0

Related Questions