如何列出CNContactPickerViewController中名称旁边的电子邮件?

时间:2016-11-14 15:47:50

标签: ios9 contacts

我像这样创建CNContactPickerViewController:

 std::cout << sspace.substr(nextQuote, endQuote-nextQuote+1) << std::endl;
除非联系人没有(在这种情况下显示电子邮件)

,否则只显示名称

如何让CNContactPickerViewController在该选择器显示的联系人表格中显示姓名旁边的电子邮件?

1 个答案:

答案 0 :(得分:0)

你不能。

借此: Swift iOS9 New Contacts Framework - How to retrieve only CNContact that has a valid email address?

//
//  ContactsTableViewController.swift

import UIKit
import Contacts
import sharedEnchantment


// word by word CNContactPickerDelegate
@available(iOS 9.0, *)
protocol ContactPickerDelegate : NSObjectProtocol {

/*!
 * @abstract Invoked when the picker is closed.
 * @discussion The picker will be dismissed automatically after a contact or property is picked.
 */
func contactPickerDidCancel(picker: ContactsPickerTableViewController)

/*!
 * @abstract Singular delegate methods.
 * @discussion These delegate methods will be invoked when the user selects a single contact or property.
 */
func contactPicker(picker: ContactsPickerTableViewController, didSelectContact contact: CNContact)
}


@available(iOS 9.0, *)
class ContactsPickerTableViewController: UITableViewController {
let keysToFetch = [CNContactGivenNameKey, CNContactFamilyNameKey, CNContactEmailAddressesKey]
let contactStore = CNContactStore()
var contacts : [CNContact] = []
/*!
 * @abstract The delegate to be notified when the user selects a contact or property.
 */
weak var delegate: ContactPickerDelegate?


override func viewDidLoad() {
    title = "Email Contacts".localized
    super.viewDidLoad()

    self.navigationController!.navigationBar.barTintColor = UIColor.whiteColor()
    navigationItem.backBarButtonItem = nil
    navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.Done,
                                                       target: self, action: #selector(TimeLineVC.cancelAndExitView(_:)))
    tableView.bounces = false
    do {
        try contactStore.enumerateContactsWithFetchRequest(CNContactFetchRequest(keysToFetch: keysToFetch)) {
            [weak self] (contact, cursor) -> Void in
            if let rawEmail = contact.emailAddresses.first,
                let email = rawEmail.value as? String where email.isValidEmail() {
                self?.contacts.append(contact)
            }
        }
    }
    catch{
        alert("contact retrieval has failed")
    }
}

func cancelAndExitView(sender: UIBarButtonItem) {
    dismissViewControllerAnimated(true) { [unowned self] in
        self.delegate?.contactPickerDidCancel(self)
    }
}

// MARK: - Table view data source
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return contacts.count
}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let ident = "subtitle"
    var cell : UITableViewCell! = tableView.dequeueReusableCellWithIdentifier(ident)
    if cell == nil {
        cell = UITableViewCell(style: .Subtitle, reuseIdentifier: ident)
    }
    let contact = contacts[indexPath.row]
    cell.textLabel?.text = contact.givenName + " " + contact.familyName // this breaks in some locales
    guard let rawEmail = contact.emailAddresses.first else {
        cell.detailTextLabel?.text = "contacts.error".localized
        return cell
    }
    let email = rawEmail.value as? String ?? ""
    cell.detailTextLabel?.text = email
    return cell
}


override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    let contact = contacts[indexPath.row]
    dismissViewControllerAnimated(true) { [unowned self] in
        self.delegate?.contactPicker(self, didSelectContact: contact)
    }
}