我是iphone开发的新手。我正在开发应用程序,我需要从地址簿中获取联系人详细信息,如姓名,电子邮件ID,电话号码以编程方式。并且这些值将存储在数组中,我们做得很好,但问题是即使是null联系人正在添加意味着如果某个人只有名称而没有电子邮件ID,那么如何检查空值不被添加。这是我正在使用的代码。
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);
}
答案 0 :(得分:0)
如果我理解正确,您不希望将没有电子邮件地址的联系人添加到阵列。您可以尝试使用if条件将联系人添加到数组
ABMultiValueRef emailAddresses = ABRecordCopyValue(ref, kABPersonEmailProperty);
if (ABMultiValueGetCount(emailAddresses) > 0)
{
[contactOfAPerson setEmail:(NSString *)ABRecordCopyValue(ref, kABPersonEmailProperty)];
[contactOfAPerson setFirstName:(NSString *)ABRecordCopyValue(ref, kABPersonFirstNameProperty)];
[contactList addObject:contactOfAPerson];
}
答案 1 :(得分:0)
查看此信息最好导入联系人及其所有详细信息
http://iphoneappcode.blogspot.in/2011/05/abpeoplepickernavigationcontroller.html