ViewController.swift MyFriend

//

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

    }

}

 

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值