Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am using Addressbook in my IOS app to fetch the contact name and numbers, but a strange thing is happening that it is showing phone number of some of the contacts.

I was hoping it will show all the contact list with phone number.

so here is my code to fetch phone numbers:

-(void)getAddressbookData
{
    #if __IPHONE_OS_VERSION_MIN_REQUIRED < 60000
        ABAddressBookRef addressBook = ABAddressBookCreate();
    #else
        CFErrorRef *error = nil;
        ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);
    #endif
        NSArray * people;
        BOOL accessGranted = [self __addressBookAccessStatus:addressBook];

        if (accessGranted)
        {
            people = (__bridge_transfer NSArray*)ABAddressBookCopyArrayOfAllPeople(addressBook);
            // Do whatever you need with thePeople...
        }
        CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);
        NSMutableArray *contactArray = [[NSMutableArray alloc] init];

        for (CFIndex i = 0; i < nPeople; i++)
        {
            ABRecordRef record = CFArrayGetValueAtIndex((__bridge CFArrayRef)(people), i);
            NSString *firstName = (__bridge NSString *)ABRecordCopyValue(record, kABPersonFirstNameProperty);
            NSString *lastName = (__bridge NSString *)ABRecordCopyValue(record, kABPersonLastNameProperty);
            NSString *fullName = nil;

            if (ABPersonGetCompositeNameFormat() == kABPersonCompositeNameFormatFirstNameFirst)
                fullName = [NSString stringWithFormat:@"%@ %@", firstName, lastName];
            else
                fullName = [NSString stringWithFormat:@"%@, %@", lastName, firstName];

            [contactArray addObject:fullName];

            //
            // Phone Numbers
            //
            ABMutableMultiValueRef phoneNumbers = ABRecordCopyValue(record, kABPersonPhoneProperty);
            CFIndex phoneNumberCount = ABMultiValueGetCount( phoneNumbers );

            NSMutableArray *numbersArray = [[NSMutableArray alloc] init];
            for ( CFIndex k=0; k<phoneNumberCount; k++ )
            {
                CFStringRef phoneNumberLabel = ABMultiValueCopyLabelAtIndex( phoneNumbers, k );
                CFStringRef phoneNumberValue = ABMultiValueCopyValueAtIndex( phoneNumbers, k );
                CFStringRef phoneNumberLocalizedLabel = ABAddressBookCopyLocalizedLabel( phoneNumberLabel );
                // converts "_$!<Work>!$_" to "work" and "_$!<Mobile>!$_" to "mobile"

                // Find the ones you want here
                //
               // NSLog(@"-----PHONE ENTRY -> name:%@ :  %@ : %@", fullName, phoneNumberLocalizedLabel, phoneNumberValue );
                [numbersArray addObject:CFBridgingRelease(phoneNumberValue)];

                CFRelease(phoneNumberLocalizedLabel);
                CFRelease(phoneNumberLabel);
                CFRelease(phoneNumberValue);
            }

           // NSLog(@"phone numbers %@", numbersArray);
            [contactDictionary setObject:numbersArray forKey:fullName];

            CFRelease(record);

        }

        selectContacts = contactArray;
       // NSLog(@"dictionary of array %@", contactDictionary);

        //NSLog(@"contacts count %d", [selectContacts count]);
    }

    -(BOOL)__addressBookAccessStatus:(ABAddressBookRef) addressBook
    {
        __block BOOL accessGranted = NO;

        if (ABAddressBookRequestAccessWithCompletion != NULL) { // we're on iOS 6
            dispatch_semaphore_t sema = dispatch_semaphore_create(0);

            ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
                accessGranted = granted;
                dispatch_semaphore_signal(sema);
            });

            dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
            // dispatch_release(sema);
        }
        else { // we're on iOS 5 or older
            accessGranted = YES;
        }
        return accessGranted;
    }

so numbersArray is blank for some of the contacts I dont know why it is happening.

like image 877
Puneet Bharti Avatar asked May 17 '13 10:05

Puneet Bharti


People also ask

How to fix iPhone contacts names not showing up?

The first thing to do is simply to restart the iPhone. This fixes the missing contacts names issue almost every time, and it’s a simple procedure. You can do this by turning it off and then turning it back on again, or you can issue a hard reboot. You can shut down iPhone through Settings and then turn it back on again too.

Why are my iCloud contacts appearing as phone numbers without names?

The next most common reason why Contacts suddenly vanish and appear as phone numbers without names attached is that somehow iCloud Contacts has been turned off, but you were previously using it.

Where are contacts stored on an iPhone?

Contacts are neither stored locally on an iPhone nor in SIM. They are synced to an email account that you had Google, iCloud, AOL, Exchange, Outlook, Yahoo or any Other If Contacts on iCloud, try this --> Restore contacts, calendars, bookmarks and more using ... Contacts are neither stored locally on an iPhone nor in SIM.

How do I enable contacts on my iPhone?

Go to Settings > tap your name to access Apple ID settings > iCloud > and look under Apps Using iCloud and make sure that “Contacts” is toggled to the ON position to be enabled.


2 Answers

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];
like image 103
SAMIR RATHOD Avatar answered Oct 24 '22 06:10

SAMIR RATHOD


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.

like image 1
ankit mehta Avatar answered Oct 24 '22 08:10

ankit mehta