我已将ABPeoplePickerNavigationController添加到我的第一个视图控制器中。我希望从地址簿中选择多个联系人,然后检索他们的号码,但我的代码只选择一个人并获取其号码,然后选择器消失。
//instantiate the person picker
let personPicker: ABPeoplePickerNavigationController
required init(coder aDecoder: NSCoder) {
personPicker = ABPeoplePickerNavigationController()
super.init(coder: aDecoder)
personPicker.peoplePickerDelegate = self
}
@IBAction func getSinglePerson(sender: AnyObject) {
self.presentViewController(personPicker, animated: true, completion: nil)
}
//later, you have handle cancel button properly.
func peoplePickerNavigationControllerDidCancel(peoplePicker: ABPeoplePickerNavigationController!) {
}
func peoplePickerNavigationController(peoplePicker: ABPeoplePickerNavigationController!, didSelectPerson person: ABRecord!) {
if peoplePicker != personPicker{
return
}
let unmanagedPhones = ABRecordCopyValue(person, kABPersonPhoneProperty)
let phones: ABMultiValueRef = Unmanaged.fromOpaque(unmanagedPhones.toOpaque()).takeUnretainedValue() as NSObject as ABMultiValueRef
let countOfPhones = ABMultiValueGetCount(phones)
for index in 0..<countOfPhones{
let unmanagedPhone = ABMultiValueCopyValueAtIndex(phones, index)
let phone: String = Unmanaged.fromOpaque(unmanagedPhone.toOpaque()).takeUnretainedValue() as NSObject as String
println(phone)
}
}
感谢任何帮助。谢谢!