Swift: Prevent ABPeoplePickerNavigationController from Closing
Asked Answered
K

1

7

I'd like to figure out a way so that, if the user presses the "Cancel" button (which I don't believe can be removed) in an ABPeoplePickerNavigationController, the view controller either doesn't close, or will be automatically reopened.

For example, given the following:

var picker = ABPeoplePickerNavigationController()
picker.peoplePickerDelegate = self
self.presentViewController(picker, animated: true, completion: nil)

I'd like to be able to do something like:

if (self.presentedViewController != picker && !userContinuedPastPicker) {
//where userContinuedPastPicker is a boolean set to false 
//in a delegate method called when the user clicks on an a contact 
//(meaning the user didn't press the cancel button but instead clicked on a contact)

    //create and present a UIAlertAction informing the user they must select a contact

    //present picker again
    self.presentViewController(picker, animated: true, completion: nil) 
}

This doesn't work; however, because the if statement won't "wait" until the user has pressed the cancel button or pressed a contact.

Koster answered 21/8, 2015 at 21:56 Comment(0)
L
4

I'm not sure there is a way to remove the cancel button, or prevent it from working, but you could respond to the func peoplePickerNavigationControllerDidCancel(_ peoplePicker: ABPeoplePickerNavigationController!) delegate to handle the case where the cancel button is pressed.

I would recommend rather than immediately reopening the picker, you open an alert telling the user they need to pick someone, then open it back up from there. It may feel broken if they cancel and it immediately opens back up.

Reference

edit:
Presenting an alert or the picker probably needs to be delayed long enough for the previous picker to close. dispatch_after

Lait answered 24/8, 2015 at 18:0 Comment(2)
This seems to be almost exactly what I need! However, when I try to present another instance of an ABPPNC or a UIAlertController (as you so wisely suggested), I get an error saying: Warning: Attempt to present <UIAlertController: 0x7a0cfc50> on <App.FirstViewController: 0x7a06dd30> whose view is not in the window hierarchy!Koster
My guess is you are presenting the alert while the people picker is animating off the screen. Try wrapping the presentViewController for the alert in a dispatch_after #24035044Lait

© 2022 - 2024 — McMap. All rights reserved.