我正在尝试获取当前用户的电子邮件(如果有),以便我可以创建自定义的“联系我们”消息。
代码是用 C 编写的。我尝试过使用 AddressBook.framework,但找不到获取电子邮件地址的方法。
有人知道如何获取电子邮件地址吗?
谢谢。
最佳答案
使用 Address Book C Framework :
#include <AddressBook/AddressBook.h>
要获取所有电子邮件地址:
ABAddressBookRef addressbook = ABGetSharedAddressBook();
ABPersonRef user = ABGetMe(addressbook);
ABMultiValueRef emails = ABRecordCopyValue(user, kABEmailProperty);
if(emails)
{
if(ABMultiValueCount(emails) != 0)
{
for(int i=0;i<ABMultiValueCount(emails);i++)
{
CFStringRef email = ABMultiValueCopyValueAtIndex(emails, i);
// Do something with current email string
CFRelease(email);
}
}
CFRelease(emails);
}
或者,检查标记为主电子邮件地址:
ABAddressBookRef addressbook = ABGetSharedAddressBook();
ABPersonRef user = ABGetMe(addressbook);
ABMultiValueRef emails = ABRecordCopyValue(user, kABEmailProperty);
if(emails)
{
if(ABMultiValueCount(emails) != 0)
{
CFStringRef primaryIdentifier = ABMultiValueCopyPrimaryIdentifier(emails);
for(int i=0;i<ABMultiValueCount(emails);i++)
{
CFStringRef currentIdentifier = ABMultiValueCopyIdentifierAtIndex(emails, i);
if(currentIdentifier==primaryIdentifier)
{
CFStringRef email = ABMultiValueCopyValueAtIndex(emails, i);
// Do something with primary email string
CFRelease(email);
}
CFRelease(currentIdentifier);
}
CFRelease(primaryIdentifier);
}
CFRelease(emails);
}
上述代码并未处理所有潜在错误,例如ABGetMe()
可以返回NULL
如果用户尚未为自己创建地址簿条目。
关于c - 在 Mac OS X 上获取默认电子邮件地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5978372/