Swift 3 中 ContactUI 的多项选择
Multiple Selections from ContactUI in Swift 3
我对在 Swift 中编码并试图自学非常陌生。我无法弄清楚如何从 Swift 中的 ContactPicker 视图 UI 启用多项选择 3.
从阅读文档来看,似乎要启用多选我应该使用 [CNContactProperty]
,但这是模棱两可的。当我这样做时,我无法调用 属性 来打印 givenName 和值,因为它们不是数组的成员。此外,当我使用 [CNContactProperty]
的语法时,我的选择器视图没有显示 "Done" 按钮来结束选择。取消是我退出选择器视图的唯一选择。
我已经找到了 Swift 以前版本的许多答案,但我对如何在 Swift 中使用此功能很感兴趣 3. 最后,我试图在 Swift 中预填充联系人字段=14=] 只需按一下发送按钮,即可将消息发送给数组中的多个联系人。
// this is the code that works for a single selection
import UIKit
import ContactsUI
import Contacts
class MainViewController: UIViewController, CNContactPickerDelegate {
// select Contacts to message from "Set Up" Page
@IBAction func pickContacts(_ sender: Any) {
let contactPicker = CNContactPickerViewController()
contactPicker.delegate = self
contactPicker.displayedPropertyKeys = [CNContactPhoneNumbersKey]
self.present(contactPicker, animated: true, completion: nil)
}
//allow contact selection and dismiss pickerView
func contactPicker(_ picker: CNContactPickerViewController, didSelect contactsProperty: CNContactProperty) {
let contact = contactsProperty.contact
let phoneNumber = contactsProperty.value as! CNPhoneNumber
print(contact.givenName)
print(phoneNumber.stringValue)
}
在您的 CNContactPickerDelegate
实施中,您实施了:
contactPicker(_ picker: CNContactPickerViewController, didSelect contactsProperty: CNContactProperty)
当特定 属性 被 selected 时调用。但是如果你想select多个联系人,你需要实现:
contactPicker(_ picker: CNContactPickerViewController, didSelect contacts: [CNContact])
returns 一组 select 联系人。所以你的委托实现方法可能看起来像这样:
func contactPicker(_ picker: CNContactPickerViewController, didSelect contacts: [CNContact]) {
for contact in contacts {
let phoneNumber = contact.value(forKey:CNContactPhoneNumbersKey)
print(contact.givenName)
print(phoneNumber)
}
}
当然,phoneNumber
变量将包含一个 phone 数字数组,您需要遍历该数组才能获得特定数字。
我对在 Swift 中编码并试图自学非常陌生。我无法弄清楚如何从 Swift 中的 ContactPicker 视图 UI 启用多项选择 3.
从阅读文档来看,似乎要启用多选我应该使用 [CNContactProperty]
,但这是模棱两可的。当我这样做时,我无法调用 属性 来打印 givenName 和值,因为它们不是数组的成员。此外,当我使用 [CNContactProperty]
的语法时,我的选择器视图没有显示 "Done" 按钮来结束选择。取消是我退出选择器视图的唯一选择。
我已经找到了 Swift 以前版本的许多答案,但我对如何在 Swift 中使用此功能很感兴趣 3. 最后,我试图在 Swift 中预填充联系人字段=14=] 只需按一下发送按钮,即可将消息发送给数组中的多个联系人。
// this is the code that works for a single selection
import UIKit
import ContactsUI
import Contacts
class MainViewController: UIViewController, CNContactPickerDelegate {
// select Contacts to message from "Set Up" Page
@IBAction func pickContacts(_ sender: Any) {
let contactPicker = CNContactPickerViewController()
contactPicker.delegate = self
contactPicker.displayedPropertyKeys = [CNContactPhoneNumbersKey]
self.present(contactPicker, animated: true, completion: nil)
}
//allow contact selection and dismiss pickerView
func contactPicker(_ picker: CNContactPickerViewController, didSelect contactsProperty: CNContactProperty) {
let contact = contactsProperty.contact
let phoneNumber = contactsProperty.value as! CNPhoneNumber
print(contact.givenName)
print(phoneNumber.stringValue)
}
在您的 CNContactPickerDelegate
实施中,您实施了:
contactPicker(_ picker: CNContactPickerViewController, didSelect contactsProperty: CNContactProperty)
当特定 属性 被 selected 时调用。但是如果你想select多个联系人,你需要实现:
contactPicker(_ picker: CNContactPickerViewController, didSelect contacts: [CNContact])
returns 一组 select 联系人。所以你的委托实现方法可能看起来像这样:
func contactPicker(_ picker: CNContactPickerViewController, didSelect contacts: [CNContact]) {
for contact in contacts {
let phoneNumber = contact.value(forKey:CNContactPhoneNumbersKey)
print(contact.givenName)
print(phoneNumber)
}
}
当然,phoneNumber
变量将包含一个 phone 数字数组,您需要遍历该数组才能获得特定数字。