博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ViewController.swift MyFriend
阅读量:6947 次
发布时间:2019-06-27

本文共 3111 字,大约阅读时间需要 10 分钟。

  hot3.png

//

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

    }

}

 

转载于:https://my.oschina.net/ldm95/blog/699372

你可能感兴趣的文章
shape用法知多少
查看>>
oracle power 函数
查看>>
网页头部<meta name="Robots" 用法 <meta>系列用法
查看>>
我的友情链接
查看>>
ubuntu apache操作-工作笔记
查看>>
我的友情链接
查看>>
分布式数据库中间件对比总结(1)
查看>>
暴风影音CEO冯鑫的人生解读
查看>>
动态控制header显示
查看>>
如何使用redhat 6.0 Enterprise企业版虚拟化安装虚拟机
查看>>
idea导出可执行jar包
查看>>
Spring中HttpInvoker远程调用使用实例
查看>>
MariaDB主从搭建与测试
查看>>
华为交换机一般设置
查看>>
入门Linux运维工程师,必须要掌握的10个技术点
查看>>
通过脚本案例学习shell(五) 通过创建DNS脚本一步一步教你将一个普通脚本规范到一个生产环境脚本...
查看>>
ZABBIX配置自动添加端口监控,并触发重启服务
查看>>
mysql 查询字段在某个数值的临近值
查看>>
Windows Phone 7 配置 Exchange ActiveSync
查看>>
sql server 所有表大小排序
查看>>