iosswiftcontactsabpeoplepickerview

Swift: Edit ABPersonViewController


I'm trying to set up my app so that when a specific contact is opened from the ABPeoplePickerNavigationController, in the ABPersonViewController, there's an edit button in the top right corner.

I know questions very similar to this have been asked here before, but I've looked at each. Perhaps partially because they were for older versions of iOS, or because most were in Objective-C, or due to my own ignorance, I haven't been able to work this out.

Here's my code:

func peoplePickerNavigationController(peoplePicker: ABPeoplePickerNavigationController!, shouldContinueAfterSelectingPerson person: ABRecord!) -> Bool {

    /*let controller = ABPersonViewController()
    controller.displayedPerson = person
    controller.allowsEditing = true
    controller.editing = true
    controller.personViewDelegate = self
    peoplePicker.pushViewController(controller, animated: true)*/

    let picker = ABNewPersonViewController()
    picker.newPersonViewDelegate = self
    picker.displayedPerson = person
    picker.navigationItem.title = "edit contact"
    self.navigationController?.pushViewController(picker, animated: true)

    return false
}

The commented code is what I had before I saw this question: iOS7 - ABPersonViewController, editing mode

Again, all I'm trying to do is add the edit button at the top so that the user has the option to edit a contact, but the ABPersonViewController should not open in edit mode.

Any help is appreciated, I'm sorry if this is tedious.


Solution

  • This should work:

    // iOS 8 and later
    func peoplePickerNavigationController(peoplePicker: ABPeoplePickerNavigationController!, didSelectPerson person: ABRecord!) {
        self.pushViewControllerWithPerson(person)
    }
    
    // Prior iOS 8
    func peoplePickerNavigationController(peoplePicker: ABPeoplePickerNavigationController!, shouldContinueAfterSelectingPerson person: ABRecord!) -> Bool {
        self.pushViewControllerWithPerson(person)
        return false
    }
    
    func pushViewControllerWithPerson(person: ABRecord) {
        let controller = ABPersonViewController()
        controller.displayedPerson = person
        controller.allowsEditing = true
        controller.personViewDelegate = self
        self.navigationController?.pushViewController(controller, animated: true)
    }
    

    Note:
    peoplePickerNavigationController(peoplePicker:shouldContinueAfterSelectingPerson:) is deprecated in iOS 8. So this method isn't called since this iOS version.
    peoplePickerNavigationController(peoplePicker:didSelectPerson:) should be used instead.