Skip to main content
Glama
Contacts+Extensions.swift4.11 kB
import Contacts import Ontology /// Helper for working with contact label constants enum CNContactLabel { static func from(string: String) -> String { switch string.lowercased() { case "mobile": return CNLabelPhoneNumberMobile case "work": return CNLabelWork case "home": return CNLabelHome default: return string } } static var allPhoneLabels: [String] { return [CNLabelPhoneNumberMobile, CNLabelWork, CNLabelHome] } static var allEmailLabels: [String] { return [CNLabelWork, CNLabelHome] } static var allAddressLabels: [String] { return [CNLabelWork, CNLabelHome] } } extension CNMutableContact { /// Populate a contact from provided arguments dictionary func populate(from arguments: [String: Value]) { // Set given name if case let .string(givenName) = arguments["givenName"] { self.givenName = givenName } // Set family name if case let .string(familyName) = arguments["familyName"] { self.familyName = familyName } // Set organization name if case let .string(organizationName) = arguments["organizationName"] { self.organizationName = organizationName } // Set job title if case let .string(jobTitle) = arguments["jobTitle"] { self.jobTitle = jobTitle } // Set phone numbers if case let .object(phoneNumbers) = arguments["phoneNumbers"] { self.phoneNumbers = phoneNumbers.compactMap { entry in guard case let .string(value) = entry.value, !value.isEmpty else { return nil } return CNLabeledValue( label: CNContactLabel.from(string: entry.key), value: CNPhoneNumber(stringValue: value) ) } } // Set email addresses if case let .object(emailAddresses) = arguments["emailAddresses"] { self.emailAddresses = emailAddresses.compactMap { entry in guard case let .string(value) = entry.value, !value.isEmpty else { return nil } return CNLabeledValue( label: CNContactLabel.from(string: entry.key), value: value as NSString ) } } // Set postal addresses if case let .object(postalAddresses) = arguments["postalAddresses"] { self.postalAddresses = postalAddresses.compactMap { entry in guard case let .object(addressData) = entry.value else { return nil } let postalAddress = CNMutablePostalAddress() if case let .string(street) = addressData["street"] { postalAddress.street = street } if case let .string(city) = addressData["city"] { postalAddress.city = city } if case let .string(state) = addressData["state"] { postalAddress.state = state } if case let .string(postalCode) = addressData["postalCode"] { postalAddress.postalCode = postalCode } if case let .string(country) = addressData["country"] { postalAddress.country = country } return CNLabeledValue( label: CNContactLabel.from(string: entry.key), value: postalAddress ) } } // Set birthday if case let .object(birthdayData) = arguments["birthday"], case let .int(day) = birthdayData["day"], case let .int(month) = birthdayData["month"] { var dateComponents = DateComponents() dateComponents.day = day dateComponents.month = month if case let .int(year) = birthdayData["year"] { dateComponents.year = year } self.birthday = dateComponents } } }

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/mattt/iMCP'

If you have feedback or need assistance with the MCP directory API, please join our Discord server