Created
March 17, 2016 14:44
-
-
Save 0x7466/26a3f6a72d0fdd468483 to your computer and use it in GitHub Desktop.
Contact class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Contact.swift | |
// Debtly | |
// | |
// Created by Tobias Feistmantl on 17/01/16. | |
// Copyright © 2016 Tobias Feistmantl. All rights reserved. | |
// | |
import Foundation | |
import Contacts | |
import PhoneNumberKit | |
class Contact { | |
private static let contactStore = CNContactStore() | |
private static let predicate = CNContact.predicateForContactsInContainerWithIdentifier(contactStore.defaultContainerIdentifier()) | |
private static let keysToFetch = [ | |
CNContactFormatter.descriptorForRequiredKeysForStyle(.FullName), | |
CNContactPhoneNumbersKey | |
] | |
private static var _allContacts: [CNContact]? | |
static func allContactsAsync(completionHandler: ([CNContact]?) -> Void) { | |
if _allContacts == nil { | |
contactStore.requestAccessForEntityType(CNEntityType.Contacts) { granted, error in | |
if granted { | |
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0)) { | |
_allContacts = try? self.contactStore.unifiedContactsMatchingPredicate(predicate, keysToFetch: keysToFetch) | |
dispatch_async(dispatch_get_main_queue()) { | |
completionHandler(_allContacts) | |
} | |
} | |
} else { | |
print("Not granted! Error: \(error)") | |
completionHandler(_allContacts) | |
} | |
} | |
} else { | |
completionHandler(_allContacts) | |
} | |
} | |
static var allContacts: [CNContact]? { | |
if _allContacts == nil { | |
_allContacts = try? self.contactStore.unifiedContactsMatchingPredicate(predicate, keysToFetch: keysToFetch) | |
} | |
return _allContacts | |
} | |
static func contactsWithPhoneNumberAsync(phoneNumber: PhoneNumber, completionHandler: ([CNContact]?) -> Void) { | |
var matchedContacts: [CNContact]? | |
allContactsAsync { contacts in | |
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0)) { | |
if let contacts = contacts { | |
matchedContacts = [] | |
for contact in contacts { | |
for number in contact.phoneNumbers { | |
let n = try? PhoneNumber(rawNumber: (number.value as! CNPhoneNumber).stringValue) | |
if let number = n { | |
if number.toE164() == phoneNumber.toE164() { | |
matchedContacts?.append(contact) | |
} | |
} | |
} | |
} | |
} | |
dispatch_async(dispatch_get_main_queue()) { | |
completionHandler(matchedContacts) | |
} | |
} | |
} | |
} | |
static func contactsWithPhoneNumber(phoneNumber: PhoneNumber) -> [CNContact]? { | |
var matchedContacts: [CNContact]? | |
if let contacts = allContacts { | |
matchedContacts = [] | |
for contact in contacts { | |
for number in contact.phoneNumbers { | |
let n = try? PhoneNumber(rawNumber: (number.value as! CNPhoneNumber).stringValue) | |
if let number = n { | |
if number.toE164() == phoneNumber.toE164() { | |
matchedContacts?.append(contact) | |
} | |
} | |
} | |
} | |
} | |
return matchedContacts | |
} | |
static func contactWithIdentifier(identifier: String) -> CNContact? { | |
return try? contactStore.unifiedContactWithIdentifier(identifier, keysToFetch: keysToFetch) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment