2016-11-14 8 views
1

私はそうのようなCNContactPickerViewControllerを作成します。CNContactPickerViewControllerの名前の隣に電子メールを表示するにはどうすればいいですか?

private var newpicker : CNContactPickerViewController? { 
    let p = CNContactPickerViewController() 
    p.displayedPropertyKeys = [ CNContactGivenNameKey, CNContactFamilyNameKey, CNContactEmailAddressesKey ] 
    p.predicateForEnablingContact = NSPredicate(format: "%[email protected] > 0", CNContactEmailAddressesKey) 
    p.delegate = self 
    return p 
} 

接触が(ケースのメールが示されている)どれ

を持っていない場合を除き名前だけが表示され、私はCNContactPickerViewControllerが内の名前の隣に電子メールを表示するために得るのですかそのピッカーによって示される連絡先テーブル?

答えて

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) 
    } 
} 
関連する問題