如何在iPhone中获取配置的电子邮件

时间:2009-03-19 10:57:57

标签: iphone cocoa-touch

我需要使用cocoa touch获取用户在iPhone中配置的电子邮件。我怎么能这样做。

感谢您的回答。

1 个答案:

答案 0 :(得分:2)

此代码将获取地址簿中的所有联系人,并输出与其关联的电子邮件地址:

// get a list of all contacts in the address book db
ABAddressBookRef ab = ABAddressBookCreate();
CFArrayRef contacts = ABAddressBookCopyArrayOfAllPeople(ab);
CFIndex nPeople = ABAddressBookGetPersonCount(ab);

for( int i = 0; i < nPeople; ++i ) {
    ABRecordRef ref = CFArrayGetValueAtIndex(contacts,i);

    // output the contact's name
    CFStringRef firstName = ABRecordCopyValue(ref, kABPersonFirstNameProperty);
    CFStringRef lastName = ABRecordCopyValue(ref, kABPersonLastNameProperty);
    NSString *contactFirstLast = [NSString stringWithFormat:@"%@,%@:",
                                    firstName, lastName];
    NSLog(@"%@", contactFirstLast);

    // output all email addresses stored for the contact
    ABMultiValueRef emailRef = ABRecordCopyValue(ref, kABPersonEmailProperty);
    CFIndex nEmails = ABMultiValueGetCount(emailRef);
    CFArrayRef emails = ABMultiValueCopyArrayOfAllValues(emailRef);
    for( int j = 0; j < nEmails; ++j ) {
        CFStringRef email = CFArrayGetValueAtIndex(emails, j);
        NSLog(@"\t%@", email);
    };

    // clean up
    CFRelease(emailRef);
    CFRelease(firstName);
    CFRelease(lastName);
}

CFRelease(contacts);