1

I have been able to use the SDK to access Address Book contacts for people, but whenever I pick a contact who is a company, my app crashes.

does anyone know the property for the company field? does anyone know the code to make it work?

thanks in advance

2
  • it's a problem with your code, but you haven't posted any.
    – wkw
    Dec 8, 2009 at 10:48
  • Betcha when you fetch the contact's name, you don't check for NULL. May 26, 2012 at 20:18

2 Answers 2

1

It seems your issue is not related to company tag itself. AB fields need to be managed carefully, released when not used and checked for nil all the time, many fields are not set.

In principle, here is example of how I use it. This function loads contacts (those fields interested for my app) and returns array with contacts to the caller. Hope this is helpful. Note how I release not needed refs.

- (NSArray *)loadContacts
{
        ABAddressBookRef addressBook = ABAddressBookCreate();
        CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
        CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);
        NSMutableArray *contacts = [NSMutableArray array];

        for (int i = 0 ; i < nPeople ; i++ ) {
                ABRecordRef person = CFArrayGetValueAtIndex(allPeople, i);
                NSString *firstName = ABRecordCopyValue(person, kABPersonFirstNameProperty);
                NSString *lastName = ABRecordCopyValue(person, kABPersonLastNameProperty);

                NSString *fullName;

                if (firstName && lastName) {
                        fullName = [NSString stringWithFormat:@"%@ %@", firstName, lastName];
                } else {
                        if (firstName) {
                                fullName = [NSString stringWithString:firstName];
                        } else if (lastName) {
                                fullName = [NSString stringWithString:lastName];
                        } else {
                                continue;
                        }
                }

                NSString *email = nil;
                ABMultiValueRef emails = ABRecordCopyValue(person, kABPersonEmailProperty);

                if (emails) {
                        NSArray *emailAddresses = [(NSArray *)ABMultiValueCopyArrayOfAllValues(emails) autorelease];
                        if (emailAddresses && [emailAddresses count] > 0)
                                email = [emailAddresses objectAtIndex:0];
                        CFRelease(emails);
                }
                if (email) {
                        NSDictionary *contact = [NSDictionary
                                                 dictionaryWithObjectsAndKeys:fullName, @"name",
                                                 email, @"email", nil];
                        [contacts addObject:contact];
                }
                if (firstName)
                        CFRelease(firstName);
                if (lastName)
                        CFRelease(lastName);
        }

        CFRelease(allPeople);
        CFRelease(addressBook);
        return contacts;
}
0

you need to use kABPersonOrganizationProperty property for get the company name from addressbook

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.