Import particular contact details from addressbook in iphone - 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);
}

Related

How to get contacts with Email from AddressBook in iOS?

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

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

How can I get the email id list using Twitter Integration in the iPhone SDK?

How can I get the Twitter email id list in the iPhone sdk?
I'm using MGTwitterEngine and when I try the following method:
NSString *str2 = [_engine getUserInformationForEmail:#"yw8181#gmail.com"];
NSLog(#"User Email Id:--->%#",str2);;
so its return 0E156D0F-3E56-4935-9BE9-2A34786545ED so how can i display in tableView
I'm not getting email id list, please help me solve this issue.
What happens when you log it?
I'm guessing that it doesn't return an NSString, but another datatype like an NSArray?
Try this:
NSArray *myArray = [_engine getUserInformationForEmail:#"yw8181#gmail.com"];
for (NSString *item in myArray) {
NSLog(#"%#",item);
}