获取Mac OS X上的默认电子邮件地址

时间:2023-01-21 14:38:32

I'm trying to get the current user's email (if any) so that I can create a customized "contact us" message.

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

The code is in C. I've tried with AddressBook.framework but I can't find a way to get the email address.

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

Anyone knows how to get the email address?
Thank you.

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

1 个解决方案

#1


4  

Using Address Book C Framework:

#include <AddressBook/AddressBook.h>

To get all email addresses:

要获取所有电子邮件地址:

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);
}

Or, to check for the email address marked as the primary one:

或者,要检查标记为主要电子邮件地址的电子邮件地址:

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);
}

Not all potential errors are handled in the above code, e.g. ABGetMe() could return NULL if the user hasn’t created an address book entry for herself.

并非所有潜在错误都在上述代码中处理,例如如果用户没有为自己创建地址簿条目,ABGetMe()可以返回NULL。

#1


4  

Using Address Book C Framework:

#include <AddressBook/AddressBook.h>

To get all email addresses:

要获取所有电子邮件地址:

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);
}

Or, to check for the email address marked as the primary one:

或者,要检查标记为主要电子邮件地址的电子邮件地址:

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);
}

Not all potential errors are handled in the above code, e.g. ABGetMe() could return NULL if the user hasn’t created an address book entry for herself.

并非所有潜在错误都在上述代码中处理,例如如果用户没有为自己创建地址簿条目,ABGetMe()可以返回NULL。