3.0 KiB
// ViewController.swift // contactlist // // Created by Greg Jacobs on 2015-09-23. // Copyright © 2015 Binary Wasteland. All rights reserved. // import UIKit import Foundation import CoreData import Contacts import AddressBook class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. let contactStore = CNContactStore() fetchRequest.unifyResults = true //true should be the default option do { try contactStore.enumerateContactsWithFetchRequest(CNContactFetchRequest(keysToFetch: [CNContactGivenNameKey, CNContactFamilyNameKey, CNContactEmailAddressesKey])) { (contact, cursor) -> Void in if (!contact.emailAddresses.isEmpty){ //Add to your array } } } catch{ print("Handle the error please") } } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } func getContacts() -> [CNContact] { let contactStore = CNContactStore() let predicate: NSPredicate = NSPredicate(format: "") let keysToFetch = [CNContactGivenNameKey, CNContactFamilyNameKey, CNContactEmailAddressesKey]
<span style="color:#000ff;">do {</span>
<span style="color:#000ff;">return try contactStore.unifiedContactsMatchingPredicate(predicate, keysToFetch: keysToFetch)</span>
<span style="color:#000ff;">} catch {</span>
<span style="color:#000ff;">return []</span>
<span style="color:#000ff;">}</span>
<span style="color:#000ff;">}</span>
}