I need to get the user emails those are configured in iPhone using cocoa touch. How can i do that .
Thank you for answers .
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);
Related
I need to be able to read the contacts' phone number from the user's address book. The problem is that if the user has chosen to sync these contacts via Facebook, they are no longer accessible via the following code (which does work for non-synced contacts):
ABMultiValueRef phones = ABRecordCopyValue(record, kABPersonPhoneProperty);
DLog(#"Found %ld phones", ABMultiValueGetCount(phones));
for(CFIndex j = 0; j < ABMultiValueGetCount(phones); j++)
{
CFStringRef phoneNumberRef = ABMultiValueCopyValueAtIndex(phones, j);
CFStringRef locLabel = ABMultiValueCopyLabelAtIndex(phones, j);
NSString *phoneLabel =(__bridge NSString*) ABAddressBookCopyLocalizedLabel(locLabel);
NSString *phoneNumber = (__bridge NSString *)phoneNumberRef;
CFRelease(phoneNumberRef);
CFRelease(locLabel);
DLog(#" - %# (%#)", phoneNumber, phoneLabel);
[numbersArr addObject:phoneNumber];
}
The log result is [Line 126] Found 0 phones
I have tried to use CFArrayRef userNumbers = ABMultiValueCopyArrayOfAllValues(phoneNumbers);
, but this also returns nothing: [Line 118] Got user numbers: (null)
So I tried to dig into the social profiles, and this also returns nothing!
// Try to get phone numbers from social profile
ABMultiValueRef profiles = ABRecordCopyValue(record, kABPersonSocialProfileProperty);
CFIndex multiCount = ABMultiValueGetCount(profiles);
for (CFIndex i=0; i<multiCount; i++) {
NSDictionary* profile = (__bridge NSDictionary*)ABMultiValueCopyValueAtIndex(profiles, i);
NSLog(#"TESTING - Profile: %#", profile);
}
DLog(#"Got profiles: %#", profiles);
CFRelease(profiles);
Yet the log entry is:
[Line 161] Got profiles: ABMultiValueRef 0x1ddbb5c0 with 0 value(s)
If the above results are all not yielding me anything, how am I supposed to know they're a Facebook user & get their phone info?
From Apple Support:
We do not have any APIs that will return a unified Address Book
contact. However, you can retrieve phone numbers of contacts that
appear as unified in Contacts on your device by first using
ABPersonCopyArrayOfAllLinkedPeople to retrieve all linked contacts,
then iterating through these contacts to fetch their respective phone
numbers. Note that phone numbers that do not appear in the UI in
Contacts will not be returned by Address Book APIs. See below for a
snippet of code that will allow you to do so:
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person
{
//Fetch all linked contacts
CFArrayRef linkedPerson = ABPersonCopyArrayOfAllLinkedPeople(person);
//Iterate through each linked contact to fetch the phone numbers
for (CFIndex i = 0; i < CFArrayGetCount(linkedPerson); i++)
{
ABRecordRef contact = CFArrayGetValueAtIndex(linkedPerson,i);
ABMutableMultiValueRef multi = ABRecordCopyValue(contact, kABPersonPhoneProperty);
for (CFIndex i = 0; i < ABMultiValueGetCount(multi); i++)
{
CFStringRef label = ABMultiValueCopyLabelAtIndex(multi, i);
CFStringRef number = ABMultiValueCopyValueAtIndex(multi, i);
CFRelease(label);
CFRelease(number);
}
CFRelease(multi);
}
CFRelease(linkedPerson);
return YES;
I am accessing all of the users contacts and emailing them through the app. I can access all of the contacts fine, if they have an email address. If they don't have an email address, then it gives me an error, I am trying to check if they have an email address before I attempt to add the address to the list of addresses. I am using this code. It gives me an error on the line of the if statement here: if ((ABMultiValueCopyValueAtIndex(email, 0) != NULL))
How can I access only the contacts who have email addresses?
NSMutableArray* contactsArray = [NSMutableArray new];
ABAddressBookRef m_addressbook = ABAddressBookCreate();
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(m_addressbook);
CFIndex nPeople = ABAddressBookGetPersonCount(m_addressbook);
for (int i=0;i < nPeople;i++)
{
NSMutableDictionary* tempContactDic = [NSMutableDictionary new];
ABRecordRef ref = CFArrayGetValueAtIndex(allPeople,i);
CFStringRef firstName, lastName;
firstName = ABRecordCopyValue(ref, kABPersonFirstNameProperty);
lastName = ABRecordCopyValue(ref, kABPersonLastNameProperty);
NSString *name = [NSString stringWithFormat:#"%# %#", firstName, lastName];
[tempContactDic setValue:name forKey:#"name"];
//fetch email id
NSString *strEmail;
ABMultiValueRef email = ABRecordCopyValue(ref, kABPersonEmailProperty);
if ((ABMultiValueCopyValueAtIndex(email, 0) != NULL))
{
CFStringRef tempEmailref = ABMultiValueCopyValueAtIndex(email, 0);
strEmail = (__bridge NSString *)tempEmailref;
[contactsArray addObject:strEmail];
}
}
The error is because you are trying to check whether there are email address available by copying the first item in the array and check if the value is NULL. But if the array is empty you can't copy any items from it.
What you should do is check if the array holds any items:
if (ABMultiValueGetCount(email) > 0) {
// There is at least one item in the email array/
}
Also be aware they your code only copies the first e-mail address for the contact, if more then 1 e-mail is set for that contact you might want to loop thru the e-mail array.
U can use this code to fetch all emails of all users from your contacts.
- (void) getEmailsOfAllContacts
{
ABAddressBookRef _addressBookRef = ABAddressBookCreate();
NSArray * allPeople = (__bridge NSArray *)ABAddressBookCopyArrayOfAllPeople(_addressBookRef);
for (id record in allPeople)
{
CFTypeRef emailProperty = ABRecordCopyValue((__bridge ABRecordRef)record, kABPersonEmailProperty);
NSArray * allEmailArray = ((__bridge NSArray *)ABMultiValueCopyArrayOfAllValues(emailProperty));
NSString * email;
for (int i = 0; i < [allEmailArray count]; i++)
{
email = [allEmailArray objectAtIndex: i];
if (isEmpty(email) == NO)
{
[_allContactsEmailArray addObject: email];
}
}
}
CFRelease(_addressBookRef);
allPeople = nil;
}
In my app i am getting contacts like this..
ABAddressBookRef addressBook = ABAddressBookCreate();
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);
for( int i=0;i< nPeople;i++)
{
ABRecordRef ref = CFArrayGetValueAtIndex(allPeople, i);
CFStringRef firstName = ABRecordCopyValue(ref, kABPersonFirstNameProperty);
.....
and so on
}
can anyone suggest me how to get the group for nPeople.
Check the following documentation
http://developer.apple.com/library/mac/#documentation/UserExperience/Reference/AddressBook/Classes/ABPerson_Class/Reference/Reference.html#//apple_ref/occ/instm/ABPerson/parentGroups
There is a method parentGroups which returns the array of groups to which the person belongs...........
Would you please have a look at me code:
(void)loadContactsFromAddressBook {
ABAddressBookRef addressBook = ABAddressBookCreate();
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
CFIndex count = ABAddressBookGetPersonCount(addressBook);
ABRecordRef person;
for (int i = 0; i < count; i++) {
person = CFArrayGetValueAtIndex(allPeople, i);
CFStringRef cfStr = ABRecordCopyValue(person, kABPersonFirstNameProperty);
NSString* firstName = (NSString*) cfStr;
[contacts addObject:firstName];
CFRelease(cfStr);
}
CFRelease(addressBook);
CFRelease(allPeople);
}
I tried to CFRelease(person) after the for block but the application crashes with “EXC_BAD_ACCESS”. If I remove CFRelease(person), the application works.
You didn't create the person, you only referenced it. Insert child support joke here.
I am importing contacts info into some textfields, but the app crashes if there are no entries for certain fields, like phone, email, etc.
Here are my textfields:
First Name
Middle Name
Last Name
Main Phone
Mobile Phone
Email Address
Website
Suppose the selected contact doesnt have a second Phone number (in this case: Mobile Phone) or there are no URL entries for the contact. The app crashes.
*ABMutableMultiValueRef phoneMulti = ABRecordCopyValue(person, kABPersonPhoneProperty);
NSMutableArray *phones = [[NSMutableArray alloc] init];
int i;
for (i = 0; i < ABMultiValueGetCount(phoneMulti); i++) {
NSString *aPhone = [(NSString*)ABMultiValueCopyValueAtIndex(phoneMulti, i) autorelease];
[phones addObject:aPhone];
}
accountPhone1TextField.text = [phones objectAtIndex:0];
accountPhone2TextField.text = [phones objectAtIndex:1];
CFRelease(phoneMulti);
[phones release];*
Or if I am trying to get an email address from the contact and it doesnt exist this will crash:
NSString *anEmail = [(NSString*)ABMultiValueCopyValueAtIndex(emailMulti, i) autorelease];
//Variable is not a CFString
***** UPDATE *****
Ended up using the following code:
ABMutableMultiValueRef emailMulti = ABRecordCopyValue(person, kABPersonEmailProperty);
NSMutableDictionary *myEmailDict = [NSMutableDictionary dictionaryWithCapacity:ABMultiValueGetCount(emailMulti)];
for (CFIndex i = 0; i < ABMultiValueGetCount(emailMulti); i++) {
emailLabel = ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(emailMulti, i));
email = ABMultiValueCopyValueAtIndex(emailMulti, i);
[myEmailDict setObject:(NSString*)email forKey:(NSString*)emailLabel];
CFRelease(email);
CFRelease(emailLabel);
}
if (phones.count > 0) {
accountPhone1TextField.text = [phones objectAtIndex:0];
}
if (phones.count > 1) {
accountPhone2TextField.text = [phones objectAtIndex:1];
}