Это полная демонстрация для извлечения всех контактов с табличным представлением.
import UIKit
import ContactsUI
import AddressBook
import Contacts
class ShowContactsVC: UIViewController,CNContactPickerDelegate,UITableViewDelegate,UITableViewDataSource
{
@IBOutlet weak var tableView: UITableView!
let peoplePicker = CNContactPickerViewController()
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
var contacts = [CNContact]()
var option : Int = 0
var userAccessGranted : Bool = false
var dataArray : NSMutableArray?
override func viewDidLoad()
{
super.viewDidLoad()
peoplePicker.delegate = self
self.checkIfUserAccessGranted()
self.tableView.delegate = self
self.tableView.dataSource = self
navigationController!.navigationBar.barTintColor = UIColor.grayColor()
if(self.userAccessGranted)
{
self.tableView.hidden = false
fetchContacts()
}
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int
{
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
if dataArray == nil {
return 0;
}
else{
return dataArray!.count
}
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCellWithIdentifier("TableCell", forIndexPath: indexPath) as! ContactsTableViewCell
let data = dataArray![indexPath.row] as! Data;
cell.lblName.text = data.name
cell.imgContact.image = data.image
return cell
}
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath)
{
cell.backgroundColor = UIColor.cyanColor()
}
func checkIfUserAccessGranted()
{
appDelegate.requestForAccess { (accessGranted) -> Void in
if accessGranted {
self.userAccessGranted = true;
}else{
self.userAccessGranted = false;
}
}
}
func fetchContacts()
{
dataArray = NSMutableArray()
let toFetch = [CNContactGivenNameKey, CNContactImageDataKey, CNContactFamilyNameKey, CNContactImageDataAvailableKey]
let request = CNContactFetchRequest(keysToFetch: toFetch)
do{
try appDelegate.contactStore.enumerateContactsWithFetchRequest(request) {
contact, stop in
print(contact.givenName)
print(contact.familyName)
print(contact.identifier)
var userImage : UIImage;
// See if we can get image data
if let imageData = contact.imageData {
//If so create the image
userImage = UIImage(data: imageData)!
}else{
userImage = UIImage(named: "no_contact_image")!
}
let data = Data(name: contact.givenName, image: userImage)
self.dataArray?.addObject(data)
}
} catch let err{
print(err)
}
self.tableView.reloadData()
}
func contactPickerDidCancel(picker: CNContactPickerViewController)
{
picker.dismissViewControllerAnimated(true, completion: nil)
self.navigationController?.popToRootViewControllerAnimated(true)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
import UIKit
class Data {
let name : String
let image : UIImage
init(name : String, image : UIImage) {
self.image = image
self.name = name
}
}