OscarTheGrouch
OscarTheGrouch

Reputation: 2384

iPhone SDK Accessing Address book company contact

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

Upvotes: 1

Views: 2861

Answers (2)

Cynichniy Bandera
Cynichniy Bandera

Reputation: 6103

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;
}

Upvotes: 1

Hector
Hector

Reputation: 3907

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

Upvotes: 0

Related Questions