获取 iOS 上所有联系人的列表
我想获取 iPhone 的所有联系人列表。
我检查了Address Book
参考,我可能错过了一些东西,但我没有看到它提供了获取联系人列表的方法。
I want to get a list of all contacts of an iPhone.
I checked Address Book
reference, I may missed something but I didn't see it provides a method to get a list of contacts.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
在我原来的答案中,在这个答案的末尾,我展示了如何在 9.0 之前的 iOS 版本中检索联系人,其方式解决了此处其他答案所带来的一些问题。
但是,如果仅支持 iOS 9 及更高版本,则应使用
Contacts
框架,以避免使用旧版AddressBook
框架时出现的一些烦人的桥接问题。因此,在 iOS 9 中,您将使用
Contacts
框架:您还需要更新
Info.plist
,添加NSContactsUsageDescription
来解释为什么您的应用程序需要访问联系人。然后执行如下操作:
以下是我的答案,如果支持 iOS 9.0 之前的 iOS 版本,则适用。
--
对您的问题以及此处提供的许多答案的一些反应(要么无法请求权限,要么无法正确处理
ABAddressBookCreateWithOptions
错误,或者泄漏):显然,导入
AddressBook
框架:或
您必须请求应用程序访问联系人的权限。例如:
注意上面,我没有使用别人建议的模式:
这是不正确的。正如您在上面看到的,您想要:
前一种模式无法正确捕获错误,而后者则可以。如果
error
不是NULL
,不要忘记CFRelease
它(或者像我一样将所有权转移给ARC),否则你会泄漏那个对象。要遍历联系人,您需要:
我想提请您注意一个相当关键的细节,即 "创建规则":
<块引用>
核心基础函数的名称表明您何时拥有返回的对象:
名称中嵌入“
Create
”的对象创建函数;名称中嵌入“
Copy
”的对象复制函数。如果您拥有一个对象,则在使用完该对象后,您有责任放弃所有权(使用 CFRelease)。
这意味着您有责任释放任何名称中带有
Create
或Copy
的 Core Foundation 函数返回的任何对象。您可以显式调用CFRelease
(就像我上面使用addressBook
和phoneNumbers
所做的那样),或者,对于支持免费桥接的对象,您可以使用__bridge_transfer
或CFBridgingRelease
将所有权转移到 ARC(正如我上面使用allPeople
、lastName
、firstName 所做的那样
和电话号码
)。静态分析器(在 Xcode 中按 shift+command+B 或从“Product”菜单中选择“Analyze”)可以识别在许多情况下,您忽略了遵守此“创建规则”而未能释放适当的对象。因此,每当编写这样的 Core Foundation 代码时,请始终通过静态分析器运行它,以确保没有任何明显的泄漏。
In my original answer, at the end of this answer, I show how to retrieve contacts in iOS versions prior to 9.0 in a manner that addresses some of the problems entailed by other answers here.
But, if only supporting iOS 9 and later, one should use the
Contacts
framework, avoiding some of the annoying bridging issues entailed when using the olderAddressBook
framework.So, in iOS 9, you'd use the
Contacts
framework:You also need to update your
Info.plist
, adding aNSContactsUsageDescription
to explain why your app requires access to contacts.And then do something like follows:
Below is my answer applicable if supporting iOS versions prior to iOS 9.0.
--
A couple of reactions to not only your question, but also many of the answers provided here (which either fail to request permission, don't handle
ABAddressBookCreateWithOptions
errors properly, or leak):Obviously, import the
AddressBook
framework:or
You must request permission for the app to access the contacts. For example:
Note that above, I have not used the pattern suggested by others:
That is not correct. As you'll see above, you want:
The former pattern will not capture the error correctly, whereas the latter will. If
error
was notNULL
, don't forget toCFRelease
it (or transfer ownership to ARC like I did) or else you'll leak that object.To iterate through the contacts, you want to:
I want to draw your attention to a fairly key detail, namely the "Create Rule":
This means that you bear responsibility for releasing any object returned by any Core Foundation function with
Create
orCopy
in the name. You can either callCFRelease
explicitly (as I did above withaddressBook
andphoneNumbers
) or, for objects that support toll-free bridging, you can transfer ownership to ARC with__bridge_transfer
orCFBridgingRelease
(as I did above withallPeople
,lastName
,firstName
, andphoneNumber
).The static analyzer (press shift+command+B in Xcode or choose "Analyze" from the "Product" menu) can identify many situations in which you neglected to observe this "Create Rule" and failed to release the appropriate objects. So, whenever writing Core Foundation code like this, always run it through the static analyzer to make sure you don't have any obvious leaks.
也许
ABPerson
函数ABAddressBookCopyArrayOfAllPeople 可以吗?示例:
Perhaps
ABPerson
function ABAddressBookCopyArrayOfAllPeople might do?Example:
使用此代码显示所有姓名+姓氏+电话号码(iOS 6)。也适用于模拟器:
Use this code to display all names + lastnames + phonenumbers (iOS 6). Works on simulator too:
确保您有正确的导入
然后您可以使用以下命令获取包含所有联系人的 CFArray 对象
Make sure you have the proper import
Then you can get a CFArray object with all contacts using
在 iOS 6 中,请确保使用
ABAddressBookCreateWithOptions
,它是ABAddressBookCreate
的更新版本In iOS 6, make sure you use
ABAddressBookCreateWithOptions
, which is the updated version ofABAddressBookCreate
更新
iOS 9.0
。 Apple 已弃用AddressBook
,现在添加了Contacts
框架:添加
CNContactStore
属性并如下定义:然后添加这些方法来读取所有联系人:
iOS 9.0 之前 =>使用
AddressBook
框架。您必须首先检查用户联系人的访问权限并请求访问权限:
Update for
iOS 9.0
. Apple has deprecatedAddressBook
and now they have addedContacts
framework:Add
CNContactStore
property and define it like this:Then add these methods to read all contacts:
Before iOS 9.0 => Use
AddressBook
framework.You have to check for access and request access to User contacts first:
感谢 mahesh 和 wzbozon,以下代码对我有用:
Thanks to mahesh and wzbozon, the following code worked for me:
斯威夫特版本:
Swift version:
查看https://github.com/heardrwt/RHAddressBook(254 颗星 01/2014)。
为 AddressBook 提供 ObjC 包装器,API 更简单。
Check out https://github.com/heardrwt/RHAddressBook (254 stars 01/2014).
Provides an ObjC wrapper for AddressBook with much simpler API.
这适用于 ios 7 和 ios 8,希望对您有所帮助............
This works for ios 7 and ios 8 , i hope its help you.............
这是使用表视图获取所有联系人的完整演示。
This is complete demo to fetch all contacts with table view.
如果你想按字母顺序排序,可以使用下面的代码。
if you want to sort as a alphabetical, you can use below code.