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
}
}
|