1

I am trying to get phone numbers from addressbook, I could get mobile, iPhone and main phone numbers but I can not take home and work. I checked out many answers in here but I still could not solve this. This is my code below.

  (BOOL)askContactsPermission {

  if (ABAddressBookRequestAccessWithCompletion) { // if in iOS 6

    // Request authorization to Address Book
    ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, NULL);

    if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) {
        ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(bool granted, CFErrorRef error) {
                    });
    }
    else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) {
        // The user has previously given access, add the contact

    }
    else {
        // The user has previously denied access
        // Send an alert telling user to change privacy setting in settings app
    }
}
else{ // if not in iOS 6
    // just get the contacts directly
}
}   

 -(void)parseAdressBook{

ABAddressBookRef addressBook = ABAddressBookCreate();
CFArrayRef allSources = ABAddressBookCopyArrayOfAllPeople( addressBook );



allContacts=[[NSArray alloc]init];

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

ABRecordRef *person;

for (CFIndex k = 0; k < ABAddressBookGetPersonCount( addressBook ); k++)
{


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

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

    NSLog(@"%lu first Name=%@",k,firstName);
    NSLog(@"%lu last name=%@",k,lastName);
    ABRecordRef aSource = CFArrayGetValueAtIndex(allSources,k);
    ABMultiValueRef phones =(__bridge ABMultiValueRef)((__bridge NSString*)ABRecordCopyValue(aSource, kABPersonPhoneProperty));


    NSString* mobileLabel;
    NSString* basic_mobile;
    NSString* work_mobile;
    NSString* home_mobile;
    NSString* strOtherMobile;

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



        mobileLabel = (__bridge NSString*)ABMultiValueCopyLabelAtIndex(phones, i);
        if([mobileLabel isEqualToString:(NSString *)kABPersonPhoneMobileLabel])
        {
            home_mobile = (__bridge NSString*)ABMultiValueCopyValueAtIndex(phones, i); //Mobile
            NSLog(@"%ld,home mobile=%@",k,home_mobile);


        }

        if ([mobileLabel isEqualToString:(NSString*)kABPersonPhoneIPhoneLabel])
        {
            basic_mobile = (__bridge NSString*)ABMultiValueCopyValueAtIndex(phones, i); //iPhone
            NSLog(@"basic_mobile=%@",basic_mobile);

        }
        if([mobileLabel isEqualToString:(NSString *)kABPersonPhoneMainLabel])
        {
            work_mobile = (__bridge NSString*)ABMultiValueCopyValueAtIndex(phones, i);  ////Main
            NSLog(@"work_mobile=%@",work_mobile);
        }



        if([mobileLabel isEqualToString:(NSString *)kABHomeLabel])
        {
            strOtherMobile = (__bridge NSString*)ABMultiValueCopyValueAtIndex(phones, i);
            NSLog(@"strOtherMobile=%@",strOtherMobile);
        }

        if([mobileLabel isEqualToString:(NSString *)kABWorkLabel])
        {
            strOtherMobile = (__bridge NSString*)ABMultiValueCopyValueAtIndex(phones, i);
            NSLog(@"strOtherMobile=%@",strOtherMobile);
        }


    }
  }
}

strOtherMobile = null by the way.

1
  • I added also permission method and true answer because i worked on it around 3 hours and so i hope other people will see it and wont spend such crazy time as me. Apr 15, 2014 at 11:25

1 Answer 1

0
if([mobileLabel isEqualToString:(NSString *)kABHomeLabel])
    {
        work_mobile = (__bridge NSString*)ABMultiValueCopyValueAtIndex(phones, i);
        NSLog(@"strOtherMobile=%@",strOtherMobile);
    }

correct the above mistake about strOtherMobile = null

if([mobileLabel isEqualToString:(NSString *)kABHomeLabel])
    {
        strOtherMobile = (__bridge NSString*)ABMultiValueCopyValueAtIndex(phones, i);
        NSLog(@"strOtherMobile=%@",strOtherMobile);
    }

instead of work_mobile use strOtherMobile

1
  • Yea thank you so much, i made a small mistake, i can see now. Apr 15, 2014 at 11:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.