从联系人(iOS)获取所有电子邮件地址 [英] Get all E-Mail addresses from contacts (iOS)

查看:206
本文介绍了从联系人(iOS)获取所有电子邮件地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道可以根据一个联系人提取联系人并查看信息。但有没有办法从您输入的电子邮件地址中获取所有电子邮件,然后将其存储在NSArray中?
这是我第一次与联系人合作,所以我对此不太了解。

I know it is possible to pull up a contact and see information based on one contact. But is there any way to get all the emails from the contacts you have entered email addresses for and then store that in a NSArray? This is my first time working with contacts so I don't know much about it.

推荐答案

是的,你可以做到这一点。你想要这样做似乎很可疑(为什么你需要这些信息?),但这并不困难。

Yes, you can do this. It seems suspicious that you would want to do this (why do you need this information?), but it isn't difficult to do.

你可以使用<$ c每次使用 ABRecordCopyVal 。代码看起来像这样(未经测试):

You can use ABAddressBookCopyArrayOfAllPeople to get an CFArrayRef with all of the people, and then you can query kABPersonEmailProperty of each using ABRecordCopyValue. The code would look something like this (untested):

ABAddressBookRef addressBook = ABAddressBookCreate();
CFArrayRef people = ABAddressBookCopyArrayOfAllPeople(addressBook);
NSMutableArray *allEmails = [[NSMutableArray alloc] initWithCapacity:CFArrayGetCount(people)];
for (CFIndex i = 0; i < CFArrayGetCount(people); i++) {
    ABRecordRef person = CFArrayGetValueAtIndex(people, i);
    ABMultiValueRef emails = ABRecordCopyValue(person, kABPersonEmailProperty);
    for (CFIndex j=0; j < ABMultiValueGetCount(emails); j++) {
        NSString* email = (NSString*)ABMultiValueCopyValueAtIndex(emails, j);
        [allEmails addObject:email];
        [email release];
    }
    CFRelease(emails);
}
CFRelease(addressBook);
CFRelease(people);

(内存分配可能稍微偏差;自从我开发Cocoa / Core以来已经有一段时间了基础代码。)

(Memory allocation may be a little off; it's been a while since I've developed Cocoa/Core Foundation code.)

但严重的是,问你为什么要这样做。只需使用Apple提供的API在适当的时间提供联系人选择器,就有可能获得更好的解决方案。

But seriously, question why you are doing this. There's a good chance that there's a better solution by just using the Apple-provided APIs to present a contact picker at appropriate times.

这篇关于从联系人(iOS)获取所有电子邮件地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆