我正在访问所有用户联系人并通过应用程序向他们发送电子邮件。如果他们有电子邮件地址,我可以访问所有联系人。如果他们没有电子邮件地址,那么它会给我一个错误,我试图在尝试将地址添加到地址列表之前检查他们是否有电子邮件地址。我正在使用此代码。它在这里的 if 语句行给了我一个错误: if ((ABMultiValueCopyValueAtIndex(email, 0) != NULL))
如何仅访问具有电子邮件地址的联系人?
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];
}
}