-2

Trying to get the phone number string out of CNContacts. I pull up a contact picker view controller and when the user selects multiple contacts, I create a message compose view controller. I need to create an array of strings to pass along as the recipients of the message compose view controller. Error comes from the following line...contactsPhoneNumber.append(phoneNumber)

func AddFriendTapped() {
    let contactPickerViewController = CNContactPickerViewController()
    contactPickerViewController.delegate = self
    presentViewController(contactPickerViewController, animated: true, completion: nil)
}


func contactPicker(picker: CNContactPickerViewController,didSelectContacts contacts: [CNContact]) {

    //check if phone can send texts, if so, continue
    if !MFMessageComposeViewController.canSendText(){

        let composeVC = MFMessageComposeViewController()
        composeVC.messageComposeDelegate = self

        //must get phone number strings from CNContact
        let phoneNumberKey = [CNContactPhoneNumbersKey]
        for contact in contacts {
            var phoneNumber = contact.dictionaryWithValuesForKeys(phoneNumberKey)
            contactsPhoneNumber.append(phoneNumber)
        }

        composeVC.recipients = contactsPhoneNumber
        composeVC.body = "Hi, test message"

        // Present the view controller modally.
        dismissViewControllerAnimated(true) {
            self.presentViewController(composeVC, animated: true, completion: nil)
        }

    }

}

func messageComposeViewController(controller: MFMessageComposeViewController,
                                  didFinishWithResult result: MessageComposeResult) {
    // Check the result or perform other tasks.

    // Dismiss the mail compose view controller.
    controller.dismissViewControllerAnimated(true, completion: nil)
}
cnichs27
  • 248
  • 3
  • 17
  • in which line does the compiler error occur? – luk2302 May 23 '16 at 14:14
  • `var phoneNumbers = contact.phoneNumbers`, no need for `dictionaryWithValuesForKeys`. That should also show you that `phoneNumbers` is an array – Sulthan May 23 '16 at 14:17
  • The error message says the value to be assigned to `phoneNumber` is a dictionary (which ***dictionary**WithValuesForKeys* implies) rather than the expected string – vadian May 23 '16 at 14:17
  • ok, @vadian, that makes sense. @Sulthan, I now get an error `Can not convert [CNlabeledValue] to expected type string` – cnichs27 May 23 '16 at 14:28
  • `[CNlabeledValue]` is an array of `CNlabeledValue` objects – since a contact can have more than one phone number – which have a `label` (work, home etc) and a `value` (the phone number) property respectively. – vadian May 23 '16 at 14:33
  • @vadian, ok, i understand, how should I separate these `CNlabeledValue` objects into separate strings? The `MFMessageComposeViewController` is expecting an array of strings – cnichs27 May 23 '16 at 14:51
  • I'm not that familiar with the `Contacts` framework but if you need an array you can use the map function `contact.phoneNumbers.map { $0.value }` and `contactsPhoneNumber.appendContentsOf(...` – vadian May 23 '16 at 14:54

1 Answers1

2

A contact can have multiple phone numbers so contact.phoneNumbers returns an array of CNlabeledValue. You need two loops, one to iterate all the contacts other to iterate all the numbers. Then you have to extract the phone number which is of type CNPhoneNumber and then convert it to string.

I have made some changes in your code. Hope it helps. :)

func contactPicker(picker: CNContactPickerViewController,didSelectContacts contacts: [CNContact]) {

    //check if phone can send texts, if so, continue
    if !MFMessageComposeViewController.canSendText(){

        let composeVC = MFMessageComposeViewController()
        composeVC.messageComposeDelegate = self

        //must get phone number strings from CNContact

        //let phoneNumberKey = [CNContactPhoneNumbersKey]


        for contact in contacts {
            let contactNumberArray = contact.phoneNumbers
            for contactNumber in contactNumberArray{
                let number = contactNumber.value as! CNPhoneNumber
                contactsPhoneNumber.append(number.stringValue)
            }
        }


        composeVC.recipients = contactsPhoneNumber
        composeVC.body = "Hi, test message"

        // Present the view controller modally.
        dismissViewControllerAnimated(true) {
            self.presentViewController(composeVC, animated: true, completion: nil)
        }

    }

}
Jush
  • 71
  • 3