1

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)")
}
Faysal Ahmed
  • 7,501
  • 5
  • 28
  • 50
  • Edit: the last uncommented line needed to be picker.present(alertController, animated: true, completion: nil), so it presents over the picker not self. HOWEVER, now I have another issue where the alertView just disappears on its own, without waiting for user to click ok. It stays popped up for about one second. How do I get it to stay popped over until user clicks ok? – Turner Thornberry Dec 07 '17 at 16:56

1 Answers1

0

Why don't you limit the user to pick only phone numbers? you can even hide all the other fields, so they will either see phone numbers, or nothing in the case that the contact do not have one (which will cause them to go to a different contact...) Check the answer here, it also mentions the filtering to phone numbers only: Contact Address book crash on iOS 10 beta In this case, the didSelect function will be called only when a phone number is selected.

kalsky
  • 469
  • 4
  • 4