mercredi 25 mars 2015

how to select multiple contacts with ABPeoplePickerNavigationController in Swift

I have added the ABPeoplePickerNavigationController into my first view controller. I want that to select multiple contacts from address book then, retrieve their numbers, but my code only selects single person and get its number then, picker disappears.



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

}


Any help is appreciated. Thank you!




Aucun commentaire:

Enregistrer un commentaire