//
// ViewController.swift
// MyFriend
import UIKit
import ContactsUI
class ViewController: UITableViewController, CNContactPickerDelegate, CNContactViewControllerDelegate {
var listContacts: [CNContact]!
override func viewDidLoad() {
super.viewDidLoad()
self.listContacts = [CNContact]()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func selectContacts(sender: AnyObject) {
let contactPicker = CNContactPickerViewController()
contactPicker.delegate = self
contactPicker.displayedPropertyKeys = [CNContactPhoneNumbersKey]
self.presentViewController(contactPicker, animated: true, completion: nil)
}
//MARK: --表视图数据源
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.listContacts.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
let contact = self.listContacts[indexPath.row]
let firstName = contact.givenName
let lastName = contact.familyName
let name = "\(firstName) \(lastName)"
cell.textLabel!.text = name
return cell
}
//MARK: --表视图委托协议
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let contactStore = CNContactStore()
let selectedContact = self.listContacts[indexPath.row]
let keysToFetch = [CNContactViewController.descriptorForRequiredKeys()]
do {
let contact = try contactStore.unifiedContactWithIdentifier(selectedContact.identifier, keysToFetch: keysToFetch)
let controller = CNContactViewController(forContact: contact)
controller.delegate = self
controller.contactStore = contactStore
controller.allowsEditing = true
controller.allowsActions = true
controller.displayedPropertyKeys = [CNContactPhoneNumbersKey, CNContactEmailAddressesKey]
self.navigationController?.pushViewController(controller, animated: true)
} catch let error as NSError {
print(error.localizedDescription)
}
}
//MARK: --实现CNContactPickerDelegate委托协议
func contactPicker(picker: CNContactPickerViewController, didSelectContact contact: CNContact) {
if !self.listContacts.contains(contact) {
self.listContacts.append(contact)
self.tableView.reloadData()
}
}
// func contactPicker(picker: CNContactPickerViewController, didSelectContacts contacts: [CNContact]) {
//
// for contact in contacts where !self.listContacts.contains(contact) {
// self.listContacts.append(contact)
// self.tableView.reloadData()
// }
//
// }
//
// func contactPicker(picker: CNContactPickerViewController, didSelectContactProperty contactProperty: CNContactProperty) {
// let contact = contactProperty.contact
// let phoneNumber = contactProperty.value as! CNPhoneNumber
//
// print(contact.givenName)
// print(phoneNumber.stringValue)
// }
//MARK: --实现CNContactViewControllerDelegate委托协议
func contactViewController(viewController: CNContactViewController, shouldPerformDefaultActionForContactProperty property: CNContactProperty) -> Bool {
return true
}
}