开发者

Getting default email address on Mac OS X

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开发者_Go百科 email address.

Anyone knows how to get the email address?

Thank you.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜