How to get contacts with Email from AddressBook in iOS? - iphone

In my app I want to get the names and emails of the phone contacts who has emails then list them in a tableview.
Is it possible to create user objects with the name and email fields which are filled by the address book of the phone then put those object in a tableview?
Any clue will be appreciated.

The approach you need to follow is :
First you need to get the reference of the AddressBook and then get all the references of contacts in an array. And then you need to enumerate the array and check whether their email property is nil, if not nil, add that contact to another array and then use that array as a datasource for the tableView :)
If you want, I can provide you with the code for checking the email entry for contact in addressBook and then filter those contacts.
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(m_addressbook);
CFIndex nPeople = ABAddressBookGetPersonCount(m_addressbook);
for (int i=0;i < nPeople;i++) {
NSMutableDictionary *dOfPerson=[NSMutableDictionary dictionary];
ABRecordRef ref = CFArrayGetValueAtIndex(allPeople,i);
//For username and surname
ABMultiValueRef phones =(NSString*)ABRecordCopyValue(ref, kABPersonPhoneProperty);
CFStringRef firstName, lastName;
firstName = ABRecordCopyValue(ref, kABPersonFirstNameProperty);
lastName = ABRecordCopyValue(ref, kABPersonLastNameProperty);
[dOfPerson setObject:[NSString stringWithFormat:#"%# %#", firstName, lastName] forKey:#"name"];
//For Email ids
ABMutableMultiValueRef eMail = ABRecordCopyValue(ref, kABPersonEmailProperty);
if(ABMultiValueGetCount(eMail) > 0) {
[dOfPerson setObject:(NSString *)ABMultiValueCopyValueAtIndex(eMail, 0) forKey:#"email"];
}
With the help of you will get to know, whether the contact has an email or not, if it has, then you can add that contact reference to other array :) Hope it helps :)

You can follow this link - http://developer.apple.com/library/ios/#documentation/ContactData/Conceptual/AddressBookProgrammingGuideforiPhone/Introduction.html
ABAddressBook class lets you to access the address book data. And you can create your own custom object to these.
kABPersonEmailProperty lets you get the email property of an ABPerson

Related

Import particular contact details from addressbook in iphone

In my app I import a contact details using addressbookui.In case of email or mobile number empty my app getting crashed.
For email try this:
ABMutableMultiValueRef eMail = ABRecordCopyValue(ref, kABPersonEmailProperty);
if(ABMultiValueGetCount(eMail) > 0)
{
NSString *personEmail = (NSString *)ABMultiValueCopyValueAtIndex(eMail, 0);
}

contact notes are not transferred when exporting/importing via the built-in vCard representation methods

Using the ABPersonCreateVCardRepresentationWithPeople method for export, and the ABPersonCreatePeopleInSourceWithVCardRepresentation for import, I have successfully transfered contact data between devices. However, the data in the contact's "notes" field isn't transfered.
Here's my export function:
+(NSData*)exportContactsToVcard:(NSArray*)contacts
{
NSMutableArray *people = [NSMutableArray arrayWithCapacity:contacts.count];
ABAddressBookRef ab = ABAddressBookCreate();
for (Contact *contact in contacts)
{
ABRecordRef person = ABAddressBookGetPersonWithRecordID(ab,contact.contactId);
[people addObject:(__bridge id)person];
}
NSData *vCard = (__bridge NSData*)ABPersonCreateVCardRepresentationWithPeople((__bridge CFArrayRef) people);
return vCard;
}
and part of my import function:
+(NSArray*)importContactsFromVcardData:(NSData*)vcardData
{
NSMutableArray *addedContactIds = [NSMutableArray array];
ABAddressBookRef addressBook = ABAddressBookCreate();
ABRecordRef defaultSource = ABAddressBookCopyDefaultSource(addressBook);
NSArray *createdPeople = (__bridge_transfer NSArray*)ABPersonCreatePeopleInSourceWithVCardRepresentation(defaultSource,(__bridge CFDataRef)vcardData);
CFErrorRef error = NULL;
for (id person in createdPeople)
{
error = NULL;
ABRecordRef personRecord = (__bridge ABRecordRef)person;
NSString *notes = (__bridge NSString *)ABRecordCopyValue(personRecord, kABPersonNoteProperty);
In the last line, notes is always nil, even if the contact had notes before it was exported. All the other standard contact fields seem to be in place.
For example, if I replace the last line with:
NSString *firstName = (__bridge NSString *)ABRecordCopyValue(personRecord, kABPersonFirstNameProperty);
the firstName string will hold the contact's first name.
Any idea how I can work around this, and get the contact notes?
Thanks.
For testing purposes, you can export a vCard from Address Book. Then drag it to TextEdit to look at the various fields.
At the bottom you'll find something like this:
NOTE:This is a note!
Also, see this link for info on the vCard format:
http://en.wikipedia.org/wiki/VCard
ABPersonCreateVCardRepresentationWithPeople will only transfer the important contact details of a vCard...thats my identifying in much tests with the vCard and the iOS import-export function. It has many mistakes, e.g. if you try to import it with social networks like facebook, it won't do it with (just look in my other thread THREADLINK )
But if you try to add some vCard Information over ABNewPersonViewController, it will work perfectly.

Getting the email count from addressbook

I am trying to get the email count of a contact from addressbook. This is what I tried.
ABMultiValueRef email = ABRecordCopyValue(person, kABPersonEmailProperty);
NSArray* emails = (NSArray*)ABMultiValueCopyArrayOfAllValues(email);
if([emails count] < 1){
//things to do
}
But I am not getting the count right(count is always nil). What am I doing wrong here? Whats the right way to do this?
Have you checked if email is null?
Anyway, this should work:
ABMultiValueRef email = ABRecordCopyValue(person, kABPersonEmailProperty);
int size = ABMultiValueGetCount(email);
The code I have given was right. The problem was someone who gave their email address in the url label which was misleading me all this time, and I could find it only when I tried to edit the addressbook. Apologies for the stupid question. Thank you.

Add contact from iPhone application bugg

I am successfully adding contact address in iPhone through my application by using below code
ABAddressBookRef addressBook = ABAddressBookCreate();
ABRecordRef Showroom = ABPersonCreate();
ABMutableMultiValueRef multiAddress = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType);
NSMutableDictionary *addressDictionary = [[NSMutableDictionary alloc] init];
[addressDictionary setObject:[NSString stringWithFormat:#"%#",Address_Bcard.text] forKey:(NSString *) kABPersonAddressStreetKey];
ABMultiValueAddValueAndLabel(multiAddress, addressDictionary, kABWorkLabel, NULL);
ABRecordSetValue(Showroom, kABPersonAddressProperty, multiAddress,#"");
CFRelease(multiAddress);
ABAddressBookAddRecord(addressBook, Showroom, nil);
ABAddressBookSave(addressBook, nil)
CFRelease(Showroom);
CFRelease(addressBook);
I am adding australian address from application to iPhone contacts eg. Sydney Australia. Instead of Sydney Australia the iPhone contact shows Sydney Australia India in addess field. How to remove this bugg.
If you don't specify the country in the address field of the contact it will automatically takes the country you live.
A way to specify the country code to set another country is:-
Change to
Settings -> General -> International -> Region Format to "Australia".
But the problem is this will change all the other contacts also.
There is also a discussion on apple site. https://discussions.apple.com/thread/2659179?start=0&tstart=0

Storing a record locator in the user's Address Book kABPersonInstantMessageProperty field

I have an app that uses the user's Address Book for contacts. I also have other information I wish to store on a per-user basis (per Address Book entry basis). I allow the user to import a single user, an Address Book group, or all of their contacts. Because I wish to continue to allow outside applications to change the users Address Book, I do not import all the information. Instead I have chosen to alter the Address Book entry for each imported user, adding a kABPersonInstantMessageProperty key. I want populate to this key with MyAppsRecordLocaterNumber#MyAppsDomain.com as the username. I figured this will immediately be seen by the end user as both a: unobtrusive, and b: a link to my app's info (hooking the outside contact info with the internal info my app adds and keeps). The only problem? I have NO idea how to add an entry to the kABPersonInstantMessageProperty key. I have figured out how to add multi-value entries such as "Home Address" but when searching stack overflow, I come up with 4 (ONLY FOUR!) entries on questions regarding this key (kABPersonInstantMessageProperty).
A portion of my code for adding "Home Address" to a person's Address Book entry is below and I admit that I have no idea how to change this over to kABPersonInstantMessageProperty.
ABMutableMultiValueRef address = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType);
NSMutableDictionary *addressDict = [[NSMutableDictionary alloc] init];
NSMutableString *street = [NSMutableString stringWithFormat:#"%i",i];
[street appendString:#" Main Street"];
[addressDict setObject:[NSString stringWithString:street] forKey:(NSString *)kABPersonAddressStreetKey];
[addressDict setObject:#"San Jose" forKey:(NSString *)kABPersonAddressCityKey];
[addressDict setObject:#"CA" forKey:(NSString *)kABPersonAddressStateKey];
NSMutableString *zip = [NSMutableString stringWithString:#"95"];
[zip appendString:[NSString stringWithFormat:#"%00i",i]];
[addressDict setObject:zip forKey:(NSString *)kABPersonAddressZIPKey];
ABMultiValueAddValueAndLabel(address, addressDict, kABHomeLabel, NULL);
ABRecordSetValue(record, kABPersonAddressProperty, address, NULL);
// add the record
ABAddressBookAddRecord(addressBook, record, NULL);
Can someone help? I would appreciate it.
GOT IT!
This is so simple! Thanks to link text who had the same issue with lack of Google-able answers on this one. Thanks, Casey!
Whereas record is the ABPerson record and all the ABCreate stuff has already been done and i is the sequential record locator integer...
ABMutableMultiValueRef im = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType);
NSMutableDictionary *imDict = [[NSMutableDictionary alloc] init];
[imDict setObject:#"MyAppName" forKey:(NSString*)kABPersonInstantMessageServiceKey];
NSMutableString *iMID = [NSMutableString stringWithFormat:#"%i",i];
[iMID appendString:#"RL#MyAppDomain"];
[imDict setObject:iMID forKey:(NSString*)kABPersonInstantMessageUsernameKey];
ABMultiValueAddValueAndLabel(im, imDict, kABHomeLabel, NULL);
[imDict release];
ABRecordSetValue(record, kABPersonInstantMessageProperty, im, NULL);