views:

157

answers:

1

Hi

I need to get the user emails those are configured in iPhone using cocoa touch. How can i do that .

Thank you for answers .

+2  A: 

This code will fetch all contacts in the address book and output the email addresses associated with them:

// get a list of all contacts in the address book db
ABAddressBookRef ab = ABAddressBookCreate();
CFArrayRef contacts = ABAddressBookCopyArrayOfAllPeople(ab);
CFIndex nPeople = ABAddressBookGetPersonCount(ab);

for( int i = 0; i < nPeople; ++i ) {
 ABRecordRef ref = CFArrayGetValueAtIndex(contacts,i);

 // output the contact's name
 CFStringRef firstName = ABRecordCopyValue(ref, kABPersonFirstNameProperty);
 CFStringRef lastName = ABRecordCopyValue(ref, kABPersonLastNameProperty);
 NSString *contactFirstLast = [NSString stringWithFormat:@"%@,%@:",
                     firstName, lastName];
 NSLog(@"%@", contactFirstLast);

 // output all email addresses stored for the contact
 ABMultiValueRef emailRef = ABRecordCopyValue(ref, kABPersonEmailProperty);
 CFIndex nEmails = ABMultiValueGetCount(emailRef);
 CFArrayRef emails = ABMultiValueCopyArrayOfAllValues(emailRef);
 for( int j = 0; j < nEmails; ++j ) {
  CFStringRef email = CFArrayGetValueAtIndex(emails, j);
  NSLog(@"\t%@", email);
 };

 // clean up
 CFRelease(emailRef);
 CFRelease(firstName);
 CFRelease(lastName);
}

CFRelease(contacts);
drewh