Does anyone know how i could use CNSaveRequest (part of the Contacts Framework) to have my app add a contact to a user’s iPhone? I’m not even sure where to get started.
Hi @Futbol_steve,
This should get you going:
import ContactsUI
let store = CNContactStore()
let contact = CNMutableContact()
contact.familyName = "lastName"
contact.givenName = "FirstName"
// Address
let address = CNMutablePostalAddress()
address.street = "Your Street"
address.city = "Your City"
address.state = "Your State"
address.postalCode = "Your ZIP/Postal Code"
address.country = "Your Country"
let home = CNLabeledValue<CNPostalAddress>(label:CNLabelHome, value:address)
contact.postalAddresses = [home]
let phoneNumber = CNLabeledValue(label: CNLabelPhoneNumberiPhone, value:CNPhoneNumber(stringValue: "510"))
phoneNumber.settingLabel("510-555-1212")
// Save
let saveRequest = CNSaveRequest()
saveRequest.add(contact, toContainerWithIdentifier: nil)
try? store.execute(saveRequest)
Blessings,
—Mark
1 Like
Wow! Thanks so much for your help. I’ll give that a try.
Mark,
I’m struggling to make this work. Frankly i think I’m in over my head. I’m brand new to this, and I can’t get any code to compile without errors. Any advice to make this code work in an example app? My goal is to be able to demonstrate that it works to some people who are skeptical that you can actually add a contact. Thank you.