斯威夫特5
&安培;
联系人选取器获取电子邮件
首先确保导入
import ContactsUI
然后确保你有一个出口到你的文本字段。
@IBOutlet var emailTextField: UITextField!
接下来,需要将联系人选择器设置为viewController的成员变量。这将保存稍后显示联系人选取器的信息。
class EmailViewController: UIViewController {
@IBOutlet var emailTextField: UITextField!
private let contactPicker = CNContactPickerViewController()
//... rest of view controller code, etc...
}
最后,您只需将此扩展添加到EmailViewController,代码如下:
extension EmailViewController: CNContactPickerDelegate {
func contactPicker(_ picker: CNContactPickerViewController, didSelect contact: CNContact) {
let emailNumberCount = contact.emailAddresses.count
//@JA - They have to have at least 1 email address
guard emailNumberCount > 0 else {
dismiss(animated: true)
//show pop up: "Selected contact does not have a number"
let alertController = UIAlertController(title: "No emails found for contact: "+contact.givenName+" "+contact.familyName, message: nil, preferredStyle: .alert)
let cancelAction = UIAlertAction(title: "Ok", style: .default, handler: {
alert -> Void in
})
alertController.addAction(cancelAction)
self.present(alertController, animated: true, completion: nil)
return
}
//@JA - If they have only 1 email it's easy. If there is many emails we want to concatenate them and separate by commas , , ...
if emailNumberCount == 1 {
setEmailFromContact(contactEmail: contact.emailAddresses[0].value as String)
} else {
let alertController = UIAlertController(title: "Select an email from contact: "+contact.givenName+" "+contact.familyName+" or select 'All' to send to every email listed.", message: nil, preferredStyle: .alert)
for i in 0...emailNumberCount-1 {
let emailAction = UIAlertAction(title: contact.emailAddresses[i].value as String, style: .default, handler: {
alert -> Void in
self.setEmailFromContact(contactEmail: contact.emailAddresses[i].value as String)
})
alertController.addAction(emailAction)
}
let allAction = UIAlertAction(title: "All", style: .destructive, handler: {
alert -> Void in
var emailConcat = ""
for i in 0...emailNumberCount-1{
if(i != emailNumberCount-1){ //@JA - Only add the , if we are not on the last item of the array
emailConcat = emailConcat + (contact.emailAddresses[i].value as String)+","
}else{
emailConcat = emailConcat + (contact.emailAddresses[i].value as String)
}
}
self.setEmailFromContact(contactEmail: emailConcat)//@JA - Sends the concatenated version of the emails separated by commas
})
alertController.addAction(allAction)
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: {
alert -> Void in
})
alertController.addAction(cancelAction)
dismiss(animated: true)
self.present(alertController, animated: true, completion: nil)
}
}
func setEmailFromContact(contactEmail: String){
emailTextField.text = contactEmail
}
func contactPickerDidCancel(_ picker: CNContactPickerViewController) {
print("contact picker canceled")
}
}
例如,要在按钮的操作事件中调用选择器,可以执行以下操作:
@IBAction func contactsButtonPressed(_ sender: UIButton) {
contactPicker.delegate = self
self.present(contactPicker, animated: true, completion: nil)
}
contactPicker.delegate = self
在我的例子中,viewController类(emailViewController)的扩展允许它访问
CNContactPickerDelegate
它需要的协议功能。