views:

73

answers:

2

I am newbie to Address book programming. I want to retrieve all email id's from address book.The issue is below code gets the all data for one record(one person). but When i add more than one contact in address book. it crushes without showing any exception.

Any suggestions? Thanks in advance.

   self.pastUrls = [[NSMutableArray alloc] init];


ABAddressBookRef addressBook = ABAddressBookCreate();

NSArray *addresses = (NSArray *) ABAddressBookCopyArrayOfAllPeople(addressBook);

// you could probably do some kind of enumeration but I'm doing old fashoined way
int i;
for(i = 0; i < [addresses count]; i++) {
    ABRecordRef record = [addresses objectAtIndex:i];


    ABMultiValueRef multiValue = ABRecordCopyValue(record, kABPersonEmailProperty);
    NSLog(@"%@",multiValue);

    int count = ABMultiValueGetCount(multiValue);
    NSLog(@"%d",count);
    int j;
    for(j = 0; j < count; j++) {
        NSString *label = (NSString *)ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(multiValue, i));
        NSString *value = (NSString *)ABMultiValueCopyValueAtIndex(multiValue, i);

        //NSLog(@"Email for %@: %@", label, value);
        [pastUrls addObject:value];


    }
}

Regards, sathish

A: 

Apple's Address Book Programming Guide for iOS includes a sample project that will get you started with general principles for accessing address book data, including email addresses.

Alex Reynolds
A: 

There are a couple of online tutorials here that should help:

http://iphone.zcentric.com/2008/09/19/access-the-address-book/

https://developer.apple.com/iphone/library/documentation/ContactData/Conceptual/AddressBookProgrammingGuideforiPhone/100-Introduction/Introduction.html

NinjaCat
Thanks Ninjacat. I am very greatful to you
sathish kumar
No problem. I see that you are not accepting answers though -- probably a good idea to go through the questions you asked and click on the check mark. This helps others looking for the same problem as you to find an accepted answer, and it raises your acceptence rate - which means that SO community will be more likely to provide timely answers.
NinjaCat