Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift: Prevent ABPeoplePickerNavigationController from Closing

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.

like image 564
Randoms Avatar asked Aug 21 '15 21:08

Randoms


1 Answers

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

like image 191
esthepiking Avatar answered Nov 13 '22 12:11

esthepiking