IOS AddressBook is not fetching all the phone number from contact list

人盡茶涼 提交于 2019-12-03 15:12:34

try this for phone no.

    ABAddressBookRef addressBook = ABAddressBookCreate();
    NSArray *people = (NSArray*)ABAddressBookCopyArrayOfAllPeople(addressBook);
    for(id person in people){
    //fetch multiple phone nos.
        ABMultiValueRef multi = ABRecordCopyValue(person, kABPersonPhoneProperty);
        for (CFIndex j=0; j < ABMultiValueGetCount(multi); j++) {
            NSString* phone = (NSString*)ABMultiValueCopyValueAtIndex(multi, j);
            [numbersArray addObject:phone];
            [phone release];
        }
    }

and you have to alloc your array before you use. in viewDidLoad method write this for alloc array

           numbersArray=[[NSMutableArray alloc] init];

I am using this code to access the mobile number from my address book and its working fine.

ABAddressBookRef addressBook = ABAddressBookCreate();
CFArrayRef allSources = ABAddressBookCopyArrayOfAllPeople( addressBook );
for (CFIndex i = 0; i < ABAddressBookGetPersonCount( addressBook ); i++)
    {
ABRecordRef aSource = CFArrayGetValueAtIndex(allSources,i);    
ABMultiValueRef phones =(NSString*)ABRecordCopyValue(aSource, kABPersonPhoneProperty);
        NSString* mobileLabel;

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

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

            if([mobileLabel isEqualToString:(NSString *)kABPersonPhoneMobileLabel])
            {
                home_mobile = [(NSString*)ABMultiValueCopyValueAtIndex(phones, i) retain];

            }

            if ([mobileLabel isEqualToString:(NSString*)kABPersonPhoneIPhoneLabel])
            {
                basic_mobile = [(NSString*)ABMultiValueCopyValueAtIndex(phones, i)retain];


            }
            if([mobileLabel isEqualToString:(NSString *)kABPersonPhoneMainLabel])
            {
                work_mobile = [(NSString*)ABMultiValueCopyValueAtIndex(phones, i)retain];

            }
        }

You can try this.

Here's the Swift version of @Samir's answer which worked for me.

var allNumbers: [AnyObject] = []
let adbk : ABAddressBook? = ABAddressBookCreateWithOptions(nil, nil).takeRetainedValue()
let people = ABAddressBookCopyArrayOfAllPeople(adbk).takeRetainedValue() as NSArray as [ABRecord]
for person in people {
    var phones: ABMultiValueRef = ABRecordCopyValue(person, kABPersonPhoneProperty).takeRetainedValue()
    for j in 0..<ABMultiValueGetCount(phones) {
        var phone: String = ABMultiValueCopyValueAtIndex(phones, j).takeRetainedValue() as String
        allNumbers.append(phone)
    }
}
println(allNumbers)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!