2

我正在尝试获取当前用户的电子邮件(如果有),以便创建自定义的“联系我们”消息。

代码在 C 中。我尝试过使用 AddressBook.framework,但找不到获取电子邮件地址的方法。

有人知道如何获取电子邮件地址吗?
谢谢你。

4

1 回答 1

4

使用通讯簿 C 框架

#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

于 2011-05-12T14:19:08.763 回答