0

I am working on a project in Swift 2.0 that requires me to use the IOS 9s ContactUI Framework. The issue that I am having is properly picking a phone number from the contacts list. When I select a phone number from a contact, the application crashes.

Here is the code that I am using to perform this task.

var delegate: NewLocationViewControllerDelegate!
var contacts = [CNContact]()

override func viewDidLoad() {
    super.viewDidLoad()
    UIApplication.sharedApplication().delegate as! AppDelegate
    // Do any additional setup after loading the view.
}//end

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

@IBAction func openContacts(sender: UIButton) {
    let contactPicker = CNContactPickerViewController()
    contactPicker.delegate = self;
    contactPicker.displayedPropertyKeys = [CNContactPhoneNumbersKey]

    self.presentViewController(contactPicker, animated: true, completion: nil)
}//end

func contactPicker(picker: CNContactPickerViewController, didSelectContactProperty contactProperty: CNContactProperty) {
    let contact = contactProperty.contact
    let phoneNumber = contactProperty.value as! CNPhoneNumber
    print(contact.givenName)
    print(phoneNumber.stringValue)
}//end
Tamás Sengel
  • 55,884
  • 29
  • 169
  • 223
bradosev
  • 11
  • 2

1 Answers1

1

The problem is that the tap on the phone number is trying to dial the phone - and on the simulator, there is no phone.

Your contactPicker:didSelectContactProperty: will never be called, because no phone number will ever be selected. Instead, tapping a phone number will try to dial that number. This is because you have not provided a predicateForSelectionOfProperty. You need to set the predicateForSelectionOfProperty to an NSPredicate that evaluates to true when the key is a phone number.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Think about what I'm telling you. Without an NSPredicate that returns `true` when the key is a phone number, you won't be able to select a phone number. Now go write that predicate and set the `predicateForSelectionOfProperty` to it. – matt Nov 09 '15 at 04:33