您的位置:首页 > 移动开发 > Swift

ViewController.swift MyFriend

2016-06-22 00:00 381 查看
//

// 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.

}

@IBAction 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

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: