user3344977
user3344977

Reputation: 3604

When new contact is added to address book on iPhone, it's first name property NSLogs as null

I have an app that is accessing the address book. The app programatically accesses the address book on viewDidLoad, pulls all of the contacts, and places their first names into an NSString object called firstName.

For some reason, if I stop using the app, add a new contact to the address book, and then start running the app again it shows that new contact's first name as null.

I looked at all of the contacts that have been working, and compared them to the new contacts I am adding in, and nothing is different. They all have "mobile" numbers and a first name.

But no matter how many new contacts I add, all of their first names print to the console as null.

Here is the code I am using:

ABAddressBookRef m_addressbook =  ABAddressBookCreateWithOptions(NULL, NULL);


ABAddressBookCopyArrayOfAllPeople(m_addressbook);


__block BOOL accessGranted = NO;
if (ABAddressBookRequestAccessWithCompletion != NULL) { // we're on iOS 6
    dispatch_semaphore_t sema = dispatch_semaphore_create(0);
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        @autoreleasepool {
            // Write your code here...
            // Fetch data from SQLite DB
        }
    });

    ABAddressBookRequestAccessWithCompletion(m_addressbook, ^(bool granted, CFErrorRef error)

    {
        accessGranted = granted;

        NSLog(@"Has access been granted?: %hhd", accessGranted);
        NSLog(@"Has there been an error? %@", error);


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


}

if (accessGranted) {


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


    int z = allContacts.count - 1;


    for (int i = 0; i < z; i++)


    {

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

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



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


        NSLog(@"FIRSTNAME: %@", firstName);

When it gets to the very last statement, NSLog(@"FIRSTNAME: %@", firstName);, all of the contacts' first names print correctly, except for any new ones that were just added.

I even ran a test, and deleted a contact that was working, and then re-added them exactly as they were, and sure enough they are now NSLogging with a null first name value.

Upvotes: 0

Views: 267

Answers (2)

Darshan Kunjadiya
Darshan Kunjadiya

Reputation: 3329

Try this function this is working for me.

 -(void)collectAddressBookContacts 
{
contactList = [[NSMutableArray alloc] init];
ABAddressBookRef addressBook = ABAddressBookCreate();
CFArrayRef people  = ABAddressBookCopyArrayOfAllPeople(addressBook);
for(int i = 0;i<ABAddressBookGetPersonCount(addressBook);i++)
{
    NSMutableDictionary *aPersonDict = [[NSMutableDictionary alloc] init];
    ABRecordRef ref = CFArrayGetValueAtIndex(people, i);
    NSString *fullName = (__bridge NSString *) ABRecordCopyCompositeName(ref);
    if (fullName) {
        [aPersonDict setObject:fullName forKey:@"fullName"];


        // collect phone numbers

        ABMultiValueRef phones = ABRecordCopyValue(ref, kABPersonPhoneProperty);
        NSString *phoneNumber=@"";
        for(CFIndex j = 0; j < ABMultiValueGetCount(phones); j++)
        {

            if ([phoneNumber isEqualToString:@""])
            {
                phoneNumber = (__bridge NSString *) ABMultiValueCopyValueAtIndex(phones, j);
            }
            else
            {
                NSString *secondnumber=(__bridge NSString *) ABMultiValueCopyValueAtIndex(phones, j);
                phoneNumber=[NSString stringWithFormat:@"%@,%@",phoneNumber,secondnumber];
            }

        }
        [aPersonDict setObject:phoneNumber forKey:@"phoneNumbers"];


        ABMultiValueRef Address = ABRecordCopyValue(ref, kABPersonAddressProperty);
        NSString *address=@"";

        for(CFIndex j = 0; j < ABMultiValueGetCount(Address); j++)
        {
            address = (__bridge NSString *) ABMultiValueCopyValueAtIndex(Address, j);

            NSMutableDictionary *dictaddress=(NSMutableDictionary *)address;
            address=[NSString stringWithFormat:@"%@,%@,%@",[dictaddress objectForKey:@"Street"],[dictaddress objectForKey:@"City"],[dictaddress objectForKey:@"Country"]];
            break;
        }


        [aPersonDict setObject:address forKey:@"address"];

        // collect emails - key "emails" will contain an array of email addresses
        ABMultiValueRef emails = ABRecordCopyValue(ref, kABPersonEmailProperty);

        NSString *email=@"";
        for(CFIndex idx = 0; idx < ABMultiValueGetCount(emails); idx++)
        {

            if ([email isEqualToString:@""])
            {
                email =  (__bridge NSString *)ABMultiValueCopyValueAtIndex(emails, idx);
            }
            else
            {
                email=[NSString stringWithFormat:@"%@,%@",email,(__bridge NSString *)ABMultiValueCopyValueAtIndex(emails, idx)];
            }
        }
        [aPersonDict setObject:email forKey:@"emails"];
        // if you want to collect any other info that's stored in the address book, it follows the same pattern.
        // you just need the right kABPerson.... property.


        [aPersonDict setObject:[UserDefaults objectForKey:@"userid"] forKey:@"userid"];
        [contactList addObject:aPersonDict];
    } else {
        // Note: I have a few entries in my phone that don't have a name set
        // Example one could have just an email address in their address book.
    }
}



}

i hope this code useful for you .

Upvotes: 0

user3344977
user3344977

Reputation: 3604

I finally figured this out. For some reason when I add new contacts in iOS7, they NSLog like this: (555)\U00a0975-5575 instead of like this: (555)975-5575.

All I had to do was use stringByReplacingOccurrencesOfString and remove the extra \U00a0. After doing some research it appears that this is an extra space that Apple adds to the phone numbers when they are added using iOS7.

Upvotes: 0

Related Questions