Sixie
Sixie

Reputation: 83

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!

Upvotes: 0

Views: 2201

Answers (1)

Lou Franco
Lou Franco

Reputation: 89172

It's not supported by the built in controller. Try this: https://github.com/tristanhimmelman/THContactPicker

Upvotes: 4

Related Questions