Created
May 4, 2018 03:08
-
-
Save akdsouza/51bce3a11cb240e224771887d2226a25 to your computer and use it in GitHub Desktop.
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
import Foundation | |
import Intents | |
class DataManager { | |
static let sharedManager = DataManager() | |
static let sharedSuiteName = "group.com.akdsouza.SiriDemo" | |
let userDefaults = UserDefaults(suiteName: sharedSuiteName) | |
func findContact(contactName: String?, with completion: ([INPerson]) -> Void) { | |
let savedContacts = userDefaults?.value(forKey: DataManager.sharedSuiteName) as? [[String: String]] | |
var matchingContacts = [INPerson]() | |
if let contacts = savedContacts { | |
for contact in contacts { | |
if let name = contact["name"]?.lowercased(), name.contains(contactName!.lowercased()) { | |
let personHandle = INPersonHandle(value: contact["number"], type: .phoneNumber) | |
matchingContacts.append(INPerson(personHandle: personHandle, nameComponents: nil, displayName: name, image: nil, contactIdentifier: nil, customIdentifier: personHandle.value)) | |
} | |
} | |
} | |
completion(matchingContacts) | |
} | |
func saveContacts(contacts: [[String: String]]) { | |
userDefaults?.set(contacts, forKey: DataManager.sharedSuiteName) | |
userDefaults?.synchronize() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment