1

I'm trying to add new contact to the iPhone Address book using ABAddressBook. Adding first/last name works fine, but I get errors when I'm trying to add country, street, city, etc. I get an error when I use kABPersonAddressCountryCodeKey or kABPersonAddressCountryKey.

func addUserToAddressBook(){
    let stat = ABAddressBookGetAuthorizationStatus()
    switch stat {
    case .Denied, .Restricted:
        print("no access to addressbook")
    case .Authorized, .NotDetermined:
        var err : Unmanaged<CFError>? = nil
        let adbk : ABAddressBook? = ABAddressBookCreateWithOptions(nil, &err).takeRetainedValue()
        if adbk == nil {
            print(err)
            return
        }
        ABAddressBookRequestAccessWithCompletion(adbk) {
            (granted:Bool, err:CFError!) in
            if granted {
                let newContact:ABRecordRef! = ABPersonCreate().takeRetainedValue()
                var success:Bool = false

                //Updated to work in Xcode 6.1
                var error: Unmanaged<CFErrorRef>? = nil

                success = ABRecordSetValue(newContact, kABPersonFirstNameProperty, self.userFirstName. , &error)
                print("Setting first name was successful? \(success)")
                success = ABRecordSetValue(newContact, kABPersonLastNameProperty, self.user.LastName, &error)
                print("Setting last name was successful? \(success)")

                // Address??????
                success = ABRecordSetValue(newContact, kABPersonAddressCountryCodeKey, self.user.CountryCode, &error) // self.user.CountryCode = "CN" - Receive an error 

                success = ABAddressBookAddRecord(adbk, newContact, &error)
                print("Contact added successful? \(success)")
                success = ABAddressBookSave(adbk, &error)
                print("Saving addressbook successful? \(success)")

            } else {
                print(err)
            }
        }
    }

}

Does anyone know how I can fix this issue?

Cong Tran
  • 1,448
  • 14
  • 30
Eugene
  • 113
  • 1
  • 7

2 Answers2

1

i don't know about ABaddressBook but if you want it in ios 9+ then you can set as follow demo with CNContact import Contacts

func createContcat()
{
     if #available(iOS 9.0, *) {

    let contact = CNMutableContact()

    contact.imageData = NSData() // The profile picture as a NSData object

    contact.givenName = "Jack"
    contact.familyName = "test"

    let homeEmail = CNLabeledValue(label:CNLabelHome, value:"jaydeep@example.com")
    let workEmail = CNLabeledValue(label:CNLabelWork, value:"j.appleseed@icloud.com")
    contact.emailAddresses = [homeEmail, workEmail]

    contact.phoneNumbers = [CNLabeledValue(
        label:CNLabelPhoneNumberiPhone,
        value:CNPhoneNumber(stringValue:"12346579"))]

    let homeAddress = CNMutablePostalAddress()
    homeAddress.street = "1 Infinite Loop"
    homeAddress.city = "Test"
    homeAddress.state = "Guj"
    homeAddress.postalCode = "12345"
        homeAddress.country = "Country"
    contact.postalAddresses = [CNLabeledValue(label:CNLabelHome, value:homeAddress)]

    let birthday = NSDateComponents()
    birthday.day = 14
    birthday.month = 2
    birthday.year = 1991  // You can omit the year value for a yearless birthday
    contact.birthday = birthday

    // Saving the newly created contact
    let store = CNContactStore()
    let saveRequest = CNSaveRequest()
    saveRequest.addContact(contact, toContainerWithIdentifier:nil)

    do {
         try store.executeSaveRequest(saveRequest)
    } catch {
        print("Something went wrong!")
    }
    }
}
Jaydeep Patel
  • 1,699
  • 17
  • 30
0

try this one for adding address e.g. citykey

                let address:ABMutableMultiValueRef = ABMultiValueCreateMutable(ABPropertyType(kABPersonAddressProperty)).takeRetainedValue()
                let value = [self.CityKey]
                let keys = [String(kABPersonAddressCityKey)]
                let add = NSDictionary(object: value, forKey: keys)
                ABMultiValueAddValueAndLabel(address,add, kABOtherLabel, nil)
                ABRecordSetValue(newContact, kABPersonAddressProperty,address, &error)

                print("Contact added successful? \(success)")
                success = ABAddressBookSave(adbk, &error)
                print("Saving addressbook successful? \(success)")