1, Address Book Access introduction
Contacts (or address books, phone books) are a database that stores information about a contact. There are two ways to achieve access to your contacts: (1)addressbook.framework Framework: No interface, code to get all contact information(2)addressbookui.framework Framework: Accessible through the system-provided contacts interface (This is how this is done in this example)
2, properties of the contact recordIn a contact record, there are many properties that are grouped into single-valued and multivalued properties. A single-valued property is a property that has only one value: such as last name, first name, department, note, and so on. A multivalued property is a collection type that contains multiple values, such as phone numbers, Email, addresses, and so on.
3, local name of multi-valued property labelIn a multivalued attribute, you include the label, value, and ID, where the label and value are repeatable, and the ID is not duplicated. For labels, we can use the Abaddressbookcopylocalizedlabel () method to switch to a local tag name (i.e., a label name that can be read, such as work, home). Otherwise, the print out is _$!4, using the Contacts Interface sampleThis article uses the Contacts interface to access contacts, and prints out the names of the contacts and all the phones that were selected. (want to use code to get a contact, or want to know more contact properties how to read can refer to my other article "Swift-access contacts contacts (using pure Code Implementation)")
5, as follows:
6, the code is as follows
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 66676869707172737475 |
import UIKit
import AddressBookUI
class ViewController
:
UIViewController ,
ABPeoplePickerNavigationControllerDelegate
{
override func viewDidLoad() {
super
.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
//弹出通讯录联系人选择界面
var picker =
ABPeoplePickerNavigationController
()
picker.peoplePickerDelegate =
self
self
.presentViewController(picker, animated:
true
) { () ->
Void in
}
}
func peoplePickerNavigationController(peoplePicker:
ABPeoplePickerNavigationController
!,
didSelectPerson person:
ABRecord
!) {
//获取姓
var lastName =
ABRecordCopyValue
(person, kABPersonLastNameProperty).takeRetainedValue()
as
!
String
println
(
"选中人的姓:\(lastName)"
)
//获取名
var firstName =
ABRecordCopyValue
(person, kABPersonFirstNameProperty).takeRetainedValue()
as
!
String
println
(
"选中人的名:\(firstName)"
)
//获取电话
var phoneValues:
ABMutableMultiValueRef
? =
ABRecordCopyValue
(person, kABPersonPhoneProperty).takeRetainedValue()
if phoneValues !=
nil {
println
(
"选中人电话:"
)
for i
in 0 ..<
ABMultiValueGetCount
(phoneValues){
// 获得标签名
var phoneLabel =
ABMultiValueCopyLabelAtIndex
(phoneValues, i).takeRetainedValue()
as CFStringRef
;
// 转为本地标签名(能看得懂的标签名,比如work、home)
var localizedPhoneLabel =
ABAddressBookCopyLocalizedLabel
(phoneLabel)
.takeRetainedValue()
as
!
String
var value =
ABMultiValueCopyValueAtIndex
(phoneValues, i)
var phone = value.takeRetainedValue()
as
!
String
println
(
"\(localizedPhoneLabel):\(phone)"
)
}
}
}
func peoplePickerNavigationController(peoplePicker:
ABPeoplePickerNavigationController
!,
didSelectPerson person:
ABRecord
!, property:
ABPropertyID
,
identifier:
ABMultiValueIdentifier
) {
}
//取消按钮点击
func peoplePickerNavigationControllerDidCancel(peoplePicker:
ABPeoplePickerNavigationController
!) {
//去除地址选择界面
peoplePicker.dismissViewControllerAnimated(
true
, completion: { () ->
Void in
})
}
func peoplePickerNavigationController(peoplePicker:
ABPeoplePickerNavigationController
!,
shouldContinueAfterSelectingPerson person:
ABRecord
!) ->
Bool {
return true
}
func peoplePickerNavigationController(peoplePicker:
ABPeoplePickerNavigationController
!,
shouldContinueAfterSelectingPerson person:
ABRecord
!, property:
ABPropertyID
,
identifier:
ABMultiValueIdentifier
) ->
Bool {
return true
}
}
|
Swift-Access contacts contacts (using the system-provided contacts interface)