I am writing a program that allows the user to pick contacts using the CNContactPickerViewController
. If the selected contact does NOT
have an associated phone number, I want to have it popover with an error and return to the ContactPickerViewController
when they hit ok. I have gone through the code with breakpoints and it is executing correctly, however it is not presenting the error pop up.
I can't for the life of me figure out why...here is my code:
func contactPicker(_ picker: CNContactPickerViewController, didSelect contact: CNContact) {
if contact.phoneNumbers.first?.value.stringValue != nil{
//@TODO: check for repeats in people array
// do something with contact
let newPerson = Person(firstName: contact.givenName,
lastName: contact.familyName,
profileImage: #imageLiteral(resourceName: "capitalizing_on_the_economic_potential_of_foreign_entrepreneurs_feature.png") )
if contact.imageDataAvailable == true{
newPerson.profileImage = UIImage(data: contact.imageData!)!
}
// this is for the full name
let fullname = "\(contact.givenName) \(contact.familyName)"
print("The selected name is: \(fullname)")
let phoneNum = contact.phoneNumbers.first?.value.stringValue
print("The selected phone num is: \(phoneNum!)")
//appends data to new activity model for prep to send back to home vc
newActivity.people.append(newPerson)
print("the people in the new activity array are: \(newActivity.people)")
peopleCollection.reloadData()
} else {
print("error has no number")
let alertController = UIAlertController(title: "Error: Person has no number!", message: "", preferredStyle: .alert)
let confirmAction = UIAlertAction(title: "Ok", style: .default, handler: {
alert -> Void in
})
//add actions to alert sheet
alertController.addAction(confirmAction)
self.present(alertController, animated: true, completion: nil)
//the code executes here correctly, but it does not present the alertController
}
//this is for phone number without dashes
//print("the selected phone number is: \((contact.phoneNumbers[0].value ).value(forKey: "digits") as! String)")
}