I am tring to get email address of ABRecordRef like this:
ABRecordRef ref = CFArrayGetValueAtIndex( allPeople, i );
NSString *email = [(NSString*) ABRecordCopyValue( ref, kABPersonEmailProperty ) autorelease];
NSLog(#"%#", email);
It returning this:
_$!<Home>!$_ (0x6840af0) - test#test.com (0x6840cc0)
What's this stuff around the email? and how can I get rid of it?Thanks.
kABPersonEmailProperty is of type kABMultiStringPropertyType. There is no single email address property, a person might have an email address for work, one for home, etc.
You can get an array of all email addresses by using ABMultiValueCopyArrayOfAllValues:
ABMultiValueRef emailMultiValue = ABRecordCopyValue(ref, kABPersonEmailProperty);
NSArray *emailAddresses = [(NSArray *)ABMultiValueCopyArrayOfAllValues(emailMultiValue) autorelease];
CFRelease(emailMultiValue);
To get the labels of the email addresses, use ABMultiValueCopyLabelAtIndex. "_$!<Home>!$" is a special constant that's defined as kABHomeLabel, there's also kABWorkLabel.
Basically more details for #omz answer. Here is the code I used that extracts home email and the name of the person:
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person
{
ABMultiValueRef emails = ABRecordCopyValue(person, kABPersonEmailProperty);
for (CFIndex i = 0; i < ABMultiValueGetCount(emails); i++) {
NSString *label = (__bridge NSString *) ABMultiValueCopyLabelAtIndex(emails, i);
if ([label isEqualToString:(NSString *)kABHomeLabel]) {
NSString *email = (__bridge NSString *) ABMultiValueCopyValueAtIndex(emails, i);
_emailTextField.text = email;
}
}
CFRelease(emails);
NSString *first = (__bridge NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
NSString *last = (__bridge NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);
if (first && first.length > 0 && last && last.length > 0) {
_nicknameTextField.text = [NSString stringWithFormat:#"%# %#", first, last];
} else if (first && first.length > 0) {
_nicknameTextField.text = first;
} else {
_nicknameTextField.text = last;
}
[self dismissModalViewControllerAnimated:YES];
return NO;
}
Try out this......
ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init];
picker.peoplePickerDelegate = self;
// Display only a person's phone, email, and birthdate
NSArray *displayedItems = [NSArray arrayWithObjects:
[NSNumber numberWithInt:kABPersonPhoneProperty],
[NSNumber numberWithInt:kABPersonEmailProperty],
[NSNumber numberWithInt:kABPersonBirthdayProperty], nil];
picker.displayedProperties = displayedItems;
Related
Right now i am using the <AddressBookUI/AddressBookUI.h>
it is working fine, it is opening the address view controller, after tapping in a contact it goes to the detail view where I can click on any property to select and get the information.
below is the code i am using right now:
- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier {
[self peoplePickerNavigationController:peoplePicker shouldContinueAfterSelectingPerson:person property:property identifier:identifier]; }
- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker {
[picker dismissModalViewControllerAnimated:YES]; }
- (BOOL)peoplePickerNavigationController: (ABPeoplePickerNavigationController *)peoplePicker
shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property
identifier:(ABMultiValueIdentifier)identifier
{
ABMutableMultiValueRef multi = ABRecordCopyValue(person, property);
CFStringRef phone1 = ABMultiValueCopyValueAtIndex(multi, identifier);
NSLog(#"phone %#", (__bridge NSString *)phone1);
CFRelease(phone1);
ABMultiValueRef fnameProperty = ABRecordCopyValue(person, kABPersonFirstNameProperty);
ABMultiValueRef lnameProperty = ABRecordCopyValue(person, kABPersonLastNameProperty);
ABMultiValueRef phoneProperty = ABRecordCopyValue(person, kABPersonPhoneProperty);
ABMultiValueRef emailProperty = ABRecordCopyValue(person, kABPersonEmailProperty);
NSArray *emailArray = (__bridge NSArray *)ABMultiValueCopyArrayOfAllValues(emailProperty);
NSArray *phoneArray = (__bridge NSArray *)ABMultiValueCopyArrayOfAllValues(phoneProperty);
NSString *name,*phone,*email;
phone = [[NSString alloc]init];
email = [[NSString alloc]init];
name = [[NSString alloc]init];
if (fnameProperty != nil) {
name = [NSString stringWithFormat:#"%#", fnameProperty];
}
if (lnameProperty != nil) {
name = [name stringByAppendingString:[NSString stringWithFormat:#" %#", lnameProperty]];
}
if ([phoneArray count] > 0) {
if ([phoneArray count] > 1) {
for (int i = 0; i < [phoneArray count]; i++) {
phone = [phone stringByAppendingString:[NSString stringWithFormat:#"%#,", [phoneArray objectAtIndex:i]]];
}
}else {
phone = [NSString stringWithFormat:#"%#", [phoneArray objectAtIndex:0]];
}
}
if ([emailArray count] > 0) {
if ([emailArray count] > 1) {
for (int i = 0; i < [emailArray count]; i++) {
email = [email stringByAppendingString:[NSString stringWithFormat:#"%#\n", [emailArray objectAtIndex:i]]];
}
}else {
email = [NSString stringWithFormat:#"%#", [emailArray objectAtIndex:0]];
}
}
//----setting txt field values
NSArray *f_name = [name componentsSeparatedByString:#" "];
if(f_name.count>1)
{
txtFirstname.text = [f_name objectAtIndex:0];
txtLastname.text = [f_name objectAtIndex:1];
}else{
txtFirstname.text = [f_name objectAtIndex:0];
}
NSArray *only_1_no = [phone componentsSeparatedByString:#","];
NSString *str = [only_1_no objectAtIndex:identifier];
NSCharacterSet *unwantedStr = [NSCharacterSet characterSetWithCharactersInString:#"+() -"];
str = [[str componentsSeparatedByCharactersInSet: unwantedStr] componentsJoinedByString: #""];
NSArray *ar = [email componentsSeparatedByString:#"\n"];
NSMutableString *abcd = [[NSMutableString alloc]init];
for (int i = 0; i<str.length; i++)
{
NSString *abc = [NSString stringWithFormat:#"%C",[phone characterAtIndex:i]];
if(i==0){
abcd =[NSMutableString stringWithFormat:#"%#",abc];
}else{
abcd = [NSMutableString stringWithFormat:#"%#%#",abcd,abc];
}
[self showmaskonnumber:abcd];
}
txtPhoneno.text = str;
txtEmail.text = [ar objectAtIndex:0];
ABPeoplePickerNavigationController *peoplePicker1 = (ABPeoplePickerNavigationController *)peoplePicker.navigationController;
[peoplePicker1 dismissModalViewControllerAnimated:YES];
[txtEmail becomeFirstResponder];
return YES;
}
my question is: Which changes should I make to this code so when I select a contact on the address list view, the selection returns the contact and all its information without showing its details?
I solved my issue by using this delegate method
- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController*)peoplePicker didSelectPerson:(ABRecordRef)person;
{ [self selectedPerson:person]; }
i m using following code for Backup Full Address Book.
CFErrorRef error = NULL;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error);
CFArrayRef peopleForBackup = ABAddressBookCopyArrayOfAllPeople(addressBook);
CFDataRef vcards = (CFDataRef)ABPersonCreateVCardRepresentationWithPeople(peopleForBackup);
NSString *vcardString = [[NSString alloc] initWithData:(NSData *)CFBridgingRelease(vcards) encoding:NSUTF8StringEncoding];
in Above code i can not get Notes from Contacts.
Please help me, how can i take a full address book as a Backup..?
get Complete Address Book ,
-(void)getAddressbookContacts
{
//Get Contacts From PhoneBook Contacts
NSString *fullName;
UIImage *img;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, nil);
NSInteger numberOfPeople = ABAddressBookGetPersonCount(addressBook);
NSArray *allPeople = CFBridgingRelease(ABAddressBookCopyArrayOfAllPeople(addressBook));
for (NSInteger i = 0; i < numberOfPeople; i++) {
ABRecordRef person = (__bridge ABRecordRef)allPeople[i];
// get phone numbers
ABMultiValueRef phoneNumbers = ABRecordCopyValue(person, kABPersonPhoneProperty);
NSString* mobile=#"";
NSString* mobileLabel;
CFIndex numberOfPhoneNumbers = ABMultiValueGetCount(phoneNumbers);
for (CFIndex i = 0; i < numberOfPhoneNumbers; i++)
{
mobileLabel = (__bridge NSString*)ABMultiValueCopyLabelAtIndex(phoneNumbers, i);
// NSLog(#"mobileLabel=%#",mobileLabel);
//Only add numbers who have saved in Mobile or IPhone (eg.Home,Work,..)
if([mobileLabel isEqualToString:(__bridge NSString *)kABPersonPhoneMobileLabel] || [mobileLabel isEqualToString:(__bridge NSString *)kABPersonPhoneIPhoneLabel])
{
NSString *phoneNumber = CFBridgingRelease(ABMultiValueCopyValueAtIndex(phoneNumbers, i));
phoneNumber = [phoneNumber stringByReplacingOccurrencesOfString:#"-" withString:#""];
phoneNumber = [phoneNumber stringByReplacingOccurrencesOfString:#" " withString:#""];
phoneNumber = [phoneNumber stringByReplacingOccurrencesOfString:#"(" withString:#""];
phoneNumber = [phoneNumber stringByReplacingOccurrencesOfString:#")" withString:#""];
phoneNumber = [phoneNumber stringByReplacingOccurrencesOfString:#" " withString:#""];
// phoneNumber = [phoneNumber stringByReplacingOccurrencesOfString:#"+" withString:#""];
// NSLog(#"phoneNumber=%#",phoneNumber);
// contact number should be mobile number or iphone
mobile = (__bridge NSString*)ABMultiValueCopyValueAtIndex(phoneNumbers, i);
NSString *firstName = CFBridgingRelease(ABRecordCopyValue(person, kABPersonFirstNameProperty));
NSString *lastName = CFBridgingRelease(ABRecordCopyValue(person, kABPersonLastNameProperty));
// ABMultiValueRef emailMultiValue = ABRecordCopyValue(person, kABPersonEmailProperty);
//Fetch Email Address of Contact
// NSArray *emailAddresses = (__bridge NSArray *)ABMultiValueCopyArrayOfAllValues(emailMultiValue);
//NSLog(#"email is %#",emailAddresses);
//Fetch Image of Contact
NSData *imgData = (__bridge NSData *)ABPersonCopyImageData(person);
if(lastName == nil)
{
fullName = firstName;
}
else
{
fullName = [firstName stringByAppendingString:#" "];
fullName = [fullName stringByAppendingString:lastName];
}
if(imgData == nil)
{
//NSLog(#"no image found");
img = [UIImage imageNamed:#"contact"];
}
else
{
img = [UIImage imageWithData:imgData];
}
NSMutableDictionary *dictContactDetails = [[NSMutableDictionary alloc]init];
[dictContactDetails setValue:fullName forKey:#"full_name"];
[dictContactDetails setValue:img forKey:#"profile_image"];
[dictContactDetails setValue:mobile forKey:#"mobile_number"];
[dictContactDetails setValue:phoneNumber forKey:#"number"];
//Add Fullname,Image & number into Array & Display it into Table
[arrayOfInviteContacts addObject:dictContactDetails];
}
}
CFRelease(phoneNumbers);
}
NSLog(#"arrayOfInviteContacts=%lu, %#",(unsigned long)arrayOfInviteContacts.count,arrayOfInviteContacts);
[tblInviteFriends reloadData];
}
As i guess this is a very simple and basic question.
i am using #import <AddressBook/AddressBook.h> to get the contact info of device and i had successfully implemented the same.
Everythings work fine when i got one phone no and one email from each contact. But when i got multiple phone no's and multiple email then getting crash on my app. I guess i am not using the proper saving method.
So i really want to know how to save one name string and one array of email(different length) and phone no(different length) as group for a contact and same for all other. so that it would not be difficult to reproduce the result later on the detail screen
CFErrorRef *error = NULL;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
CFIndex numberOfPeople = ABAddressBookGetPersonCount(addressBook);
for(int i = 0; i < numberOfPeople; i++) {
ABRecordRef person = CFArrayGetValueAtIndex( allPeople, i );
NSString *firstName = (__bridge NSString *)(ABRecordCopyValue(person, kABPersonFirstNameProperty));
NSString *lastName = (__bridge NSString *)(ABRecordCopyValue(person, kABPersonLastNameProperty));
[values1 addObject:[NSString stringWithFormat:#"%# %#",firstName,lastName]]; //values1 array to save name of contact
ABMultiValueRef emails = ABRecordCopyValue(person, kABPersonEmailProperty);
for (CFIndex j=0; j < ABMultiValueGetCount(emails); j++) {
NSString* email = (__bridge NSString*)ABMultiValueCopyValueAtIndex(emails, j);
NSLog(#"Email:%#", email);
[values2 addObject:[NSString stringWithFormat:#"%#",email]];
} //values2 array to save email associated to that contact
ABMultiValueRef phoneNumbers = ABRecordCopyValue(person, kABPersonPhoneProperty);
for (CFIndex i = 0; i < ABMultiValueGetCount(phoneNumbers); i++) {
NSString *phoneNumber = (__bridge_transfer NSString *) ABMultiValueCopyValueAtIndex(phoneNumbers, i);
NSLog(#"phone:%#", phoneNumber);
[values3 addObject:[NSString stringWithFormat:#"%#",phoneNumber]];
}//values3 array to save different phone no's associated to that contacts
NSDictionary *dict = [[NSDictionary alloc]init];
}
AS now i have three array with details contain details of one contact. Now how can merge these three array values to one so that it would be easy to save and fetch data for multiples or hundreds of contacts
ABAddressBookRef allPeople = ABAddressBookCreate();
CFArrayRef allContacts = ABAddressBookCopyArrayOfAllPeople(allPeople);
CFIndex numberOfContacts = ABAddressBookGetPersonCount(allPeople);
NSLog(#"numberOfContacts------------------------------------%ld",numberOfContacts);
for(int i = 0; i < numberOfContacts; i++){
NSString* name = #"";
NSString* phone = #"";
NSString* email = #"";
ABRecordRef aPerson = CFArrayGetValueAtIndex(allContacts, i);
ABMultiValueRef fnameProperty = ABRecordCopyValue(aPerson, kABPersonFirstNameProperty);
ABMultiValueRef lnameProperty = ABRecordCopyValue(aPerson, kABPersonLastNameProperty);
ABMultiValueRef phoneProperty = ABRecordCopyValue(aPerson, kABPersonPhoneProperty);\
ABMultiValueRef emailProperty = ABRecordCopyValue(aPerson, kABPersonEmailProperty);
NSArray *emailArray = (__bridge NSArray *)ABMultiValueCopyArrayOfAllValues(emailProperty);
NSArray *phoneArray = (__bridge NSArray *)ABMultiValueCopyArrayOfAllValues(phoneProperty);
if (fnameProperty != nil) {
name = [NSString stringWithFormat:#"%#", fnameProperty];
}
if (lnameProperty != nil) {
name = [name stringByAppendingString:[NSString stringWithFormat:#" %#", lnameProperty]];
}
if ([phoneArray count] > 0) {
if ([phoneArray count] > 1) {
for (int i = 0; i < [phoneArray count]; i++) {
phone = [phone stringByAppendingString:[NSString stringWithFormat:#"%#\n", [phoneArray objectAtIndex:i]]];
}
}else {
phone = [NSString stringWithFormat:#"%#", [phoneArray objectAtIndex:0]];
}
}
if ([emailArray count] > 0) {
if ([emailArray count] > 1) {
for (int i = 0; i < [emailArray count]; i++) {
email = [email stringByAppendingString:[NSString stringWithFormat:#"%#\n", [emailArray objectAtIndex:i]]];
}
}else {
email = [NSString stringWithFormat:#"%#", [emailArray objectAtIndex:0]];
}
}
NSLog(#"NAME : %#",name);
NSLog(#"PHONE: %#",phone);
NSLog(#"EMAIL: %#",email);
NSLog(#"\n");
}
You can see multiple contact and email in log if contact have.
I've been looking the web for this solution. Its exactly like the google maps application on the iphone, where as you search, it looks through the address book to find suggestions for what your looking for.
What i managed to do so far is set up a search bar and a mapview, so when i search for the exact address i get it displayed on the map.
I want to make it easier for the users, who might have addresses stored in their address book to use it to search on the map view.
I know that you need to use search display controller and address book, but i can't find any examples of search display controller on other than a default table view.
this is what i have so far
EDIT.
I guess no one knows. But it took me a couple of days to figure it out.
Heres the solution for everyone wanting to learn how to do this.
The key thing to know here is how to get the address book data into an array that you can just use with the search display controller to filter it out.
So here is the method i came up with to get the address book data:
- (void) getContactData
{
NSMutableDictionary *myAddressBook = [[NSMutableDictionary alloc] init];
self.names = [NSMutableArray array];
ABAddressBookRef addressBook = ABAddressBookCreate();
if (addressBook != nil){
NSLog(#"Successfully accessed the address book.");
NSArray *allPeople = (__bridge_transfer NSArray *) ABAddressBookCopyArrayOfAllPeople(addressBook);
NSString *address;
NSUInteger peopleCounter = 0;
for (peopleCounter = 0;
peopleCounter < [allPeople count]; peopleCounter++){
ABRecordRef thisPerson = (__bridge ABRecordRef)
[allPeople objectAtIndex:peopleCounter];
NSString *firstName = (__bridge_transfer NSString *) ABRecordCopyValue(thisPerson, kABPersonFirstNameProperty);
NSString *lastName = (__bridge_transfer NSString *) ABRecordCopyValue(thisPerson, kABPersonLastNameProperty);
NSString *company = (__bridge_transfer NSString *) ABRecordCopyValue(thisPerson, kABPersonOrganizationProperty);
ABMutableMultiValueRef multiValue = ABRecordCopyValue(thisPerson, kABPersonAddressProperty);
for(CFIndex i=0;i<ABMultiValueGetCount(multiValue);i++)
{
NSString* HomeLabel = (__bridge NSString*)ABMultiValueCopyLabelAtIndex(multiValue, i);
if([HomeLabel isEqualToString:#"_$!<Home>!$_"])
{
CFDictionaryRef dict = ABMultiValueCopyValueAtIndex(multiValue, i);
CFStringRef street = CFDictionaryGetValue(dict, kABPersonAddressStreetKey);
CFStringRef city = CFDictionaryGetValue(dict, kABPersonAddressCityKey);
CFStringRef state = CFDictionaryGetValue(dict, kABPersonAddressStateKey);
CFStringRef zip = CFDictionaryGetValue(dict, kABPersonAddressZIPKey);
CFRelease(dict);
if (zip == nil) {
address = [NSString stringWithFormat:#"%#, %#, %# ", street, city, state];
}else {
address = [NSString stringWithFormat:#"%#, %#, %# , %#", street, city, state, zip];
}
}
if ([address length] != 0) {
// [self.addresses insertObject:address atIndex:i];
[myAddressBook setObject:address forKey:#"Address"];
if ([firstName length] != 0 && [lastName length] != 0) {
NSString *name= [NSString stringWithFormat: #"%# %#", firstName, lastName];
[myAddressBook setObject:name forKey:#"Name"];
// [names insertObject:name atIndex:i];
}
else if ([firstName length] != 0 && [lastName length] == 0) {
NSString *name= [NSString stringWithFormat: #"%#", firstName];
// [names insertObject:name atIndex:i];
[myAddressBook setObject:name forKey:#"Name"];
}
else if ([firstName length] == 0 && [lastName length] == 0) {
if ([company length] != 0) {
//[names insertObject:company atIndex:i];
[myAddressBook setObject:company forKey:#"Name"];
}
}
NSString *name = [myAddressBook objectForKey:#"Name"];
NSString *address = [myAddressBook objectForKey:#"Address"];
NSDictionary* personDict = [[NSDictionary alloc] initWithObjectsAndKeys:name,#"Name",address,#"Address",nil];
[self.names addObject:personDict];
}
}
}
CFRelease(addressBook);
}
}
// Setup SearchBar
UISearchBar *search = [[UISearchBar alloc] initWithFrame:CGRectMake(20, 0, 278, 44)];
search.delegate = self;
self.searchBar = search;
// Setup Map View
MKMapView *mapView = [[MKMapView alloc] initWithFrame:CGRectMake(20, 44, 278, 150)];
mapView.mapType = MKMapTypeStandard;
mapView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.myMapview = mapView;
[self.view addSubview:self.myMapview];
[self.view addSubview:self.searchBar];
- (void)searchBarSearchButtonClicked:(UISearchBar *)aSearchBar {
// When the search button is tapped, add the search term to recents and conduct the search.
NSString *searchString = [searchBar text];
self.eventLocationCoordinate = [self getLocationFromAddressString:searchString];
[self zoomMapAndCenterAtLatitude:self.eventLocationCoordinate.latitude andLongitude:self.eventLocationCoordinate.longitude];
[self setMapAnnotationAtCoordinate:self.eventLocationCoordinate withTitle:#"Your Here" andSubtitle:searchString];
}
Have fun coding!
Overview: I am trying to get the phone number and full name when people use the peoplePicker and clicks on the name. Then, I'd like to display the full name on a textfield and save the phone number as a string. Using the ph num and name, I intend to use that as a unique identification. I don't want to use the unique id of ABRecord because sometimes i have duplicates on my contacts especially when I sync it with google, etc...
If I understand correctly, I need to use this delegate method
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker
shouldContinueAfterSelectingPerson:(ABRecordRef)person
Using the above, I can get the full name to display on the text field as scuh
textField.text = ABRecordCopyCompositeName(person);
But, I don't know how to get the ph number. For me to get the ph number, I have to use the other delegate method:
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker
shouldContinueAfterSelectingPerson:(ABRecordRef)person
property:(ABPropertyID)property
identifier:(ABMultiValueIdentifier)identifier
ABMultiValueRef phoneProperty = ABRecordCopyValue(person,property);
NSString *phone = (NSString *)ABMultiValueCopyValueAtIndex(phoneProperty,identifier);
However, I don't like this because then when the user clicks on the name on the address book, it shows the detail with the ph number, email, etc and the user has to click on the ph number. What I want is from the first screen, the user clicks on the name and the name gets displayed as a textField and the ph number is saved as a string somewhere.
I use a method to save all persons with emails in a array and then display the array in a table view:
ABAddressBookRef addressBook = ABAddressBookCreate();
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);
NSMutableArray *mArr = [[NSMutableArray alloc]init];
for( int i = 0 ; i < nPeople ; i++ )
{
ABRecordRef person = CFArrayGetValueAtIndex(allPeople, i);
NSString *preName = (NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
NSString *postName = (NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);
ABMultiValueRef emails = (ABMultiValueRef) ABRecordCopyValue(person, kABPersonEmailProperty);
int ecount = ABMultiValueGetCount(emails);
for (int i = 0; i < ecount; i++) {
NSMutableDictionary *dd = [[NSMutableDictionary alloc]init];
[dd setValue:[NSString stringWithFormat:#"%# %#", preName, postName] forKey:#"name"];
NSString *emailID = (NSString *)ABMultiValueCopyValueAtIndex(emails, i);
[dd setValue:emailID forKey:#"mail"];
//NSLog(#"inside loop %# %# %#", preName, postName, emailID);
[emailID release];
[mArr addObject:dd];
[dd release];
}
}
emailArray = [[NSArray alloc]initWithArray:mArr];
[mArr release];
If you want to get just the first email and phone number use this code. This is for iOS 5.0 with Xcode 4.2
- (BOOL)peoplePickerNavigationController:
(ABPeoplePickerNavigationController *)peoplePicker
shouldContinueAfterSelectingPerson:(ABRecordRef)person
{
//NSLog(#"Went here 1 ...");
NSString *nameStr = (__bridge NSString *)ABRecordCopyCompositeName(person);
ABMultiValueRef emails = (ABMultiValueRef) ABRecordCopyValue(person, kABPersonEmailProperty);
ABMultiValueRef phones = (ABMultiValueRef) ABRecordCopyValue(person, kABPersonPhoneProperty);
NSString *emailStr = (__bridge NSString *)ABMultiValueCopyValueAtIndex(emails, 0);
NSString *phoneStr = (__bridge NSString *)ABMultiValueCopyValueAtIndex(phones, 0);
//strip number from brakets
NSMutableString *tmpStr1 = [NSMutableString stringWithFormat:#"%#", phoneStr];
NSString *strippedStr1 = [tmpStr1 stringByReplacingOccurrencesOfString:#" " withString:#""];
NSCharacterSet *doNotWant = [NSCharacterSet characterSetWithCharactersInString:#"()-"];
strippedStr1 = [[strippedStr1 componentsSeparatedByCharactersInSet: doNotWant] componentsJoinedByString: #""];
NSLog(#"nameStr: %# ... emailStr: %# ... phoneStr: %# ...", nameStr, emailStr,strippedStr1);
//dismiss
[self dismissModalViewControllerAnimated:YES];
return NO;
}