i am new to iphone development. i am developing app were i need to fetch contact details from the address book, like firstname, email id,phone numbers programmatically. And these values to be store in the array, which iam doing well, but the problem is even null contacts are getting added means if some one has only name and no email id then how to check the null vales not to be added. This is the code I am using.
ABRecordRef ref;
ABAddressBookRef m_addressbook = ABAddressBookCreate();
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(m_addressbook);
CFIndex nPeople = ABAddressBookGetPersonCount(m_addressbook);
NSLog(@" n people count values %ld",nPeople);
for (int i=0; i<nPeople; i++)
{
AFContacts *contactOfAPerson = [[AFContacts alloc] init];
ref = CFArrayGetValueAtIndex(allPeople,i);
[contactOfAPerson setEmail:(NSString *)ABRecordCopyValue(ref, kABPersonEmailProperty)];
[contactOfAPerson setFirstName:(NSString *)ABRecordCopyValue(ref, kABPersonFirstNameProperty)];
//[contactOfAPerson setPhoneNumber:(NSString *)ABRecordCopyValue(ref, kABPersonPhoneMobileLabel)];
[contactList addObject:contactOfAPerson];
[contactOfAPerson release];
}
CFRelease(ref);
}
If I understand correctly, you don’t want to add contacts that don’t have email address to the array. You can try using if condition to add contact to the array