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
}
}
// 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
}
}
相关文章推荐
- Swift学习(二)
- swift学习笔记4——扩展、协议
- Swift 3.0开发者预览版(第一版)发布内容简介
- iOS动画学习笔记5 Video Tutorial iOS Animation with Swift - Ray Wenderlich
- swift中字符串的处理
- Facebook第三方登录和分享
- iOS动画学习笔记4 Video Tutorial iOS Animation with Swift - Ray Wenderlich
- Swift - 让标签栏按钮UITabBarItem图片居中(没有文字)
- Swift - 打开第三方应用,并传递参数(附常用App的URL Scheme)
- Swift - 二维码QRCode的读取(从图片读取 ,或通过摄像头扫描)
- Swift - 给图片添加图片水印
- Swift - 制作一个录音机(声音的录制与播放)
- Swift - 使用AVKit播放本地视频,在线视频(AVPlayerViewController)
- Swift - 访问通讯录联系人(使用系统提供的通讯录交互界面)
- Swift - 使用MapKit显示地图,并在地图上做标记
- Swift - 使用NSUserDefaults来进行本地数据存储
- Swift - 使用EventKit获取系统日历事件,添加事件
- Swift - UIColor使用自定义的RGB配色
- Swift - 搜索条(UISearchBar)的用法
- Swift - 给表格的单元格UITableViewCell添加图片