3

I am creating an application where it is needed to add a contact to the address book of that device. When I am adding the contact to the device using only the first and last name, everything is going fine. However when I am trying to add the phone number as well, the app crashes. Can anybody see what I am doing wrong here?

Thanks in advance!

    let firstName = "Firstname"
    let lastName = "Lastname"
    let telephoneNumber = "1234567890"
    let notes = "This is a note"

    let person: ABRecordRef = ABPersonCreate().takeRetainedValue()

    let couldSetFirstName = ABRecordSetValue(person, kABPersonFirstNameProperty, firstName as CFTypeRef, nil)

    let couldSetLastName = ABRecordSetValue(person, kABPersonLastNameProperty, lastName as CFTypeRef, nil)
    let couldSetPhoneNumber = ABRecordSetValue(person, kABPersonPhoneProperty, telephoneNumber as CFTypeRef, nil)

    let couldSetNotes = ABRecordSetValue(person, kABPersonNoteProperty, notes, nil)

    var error: Unmanaged<CFErrorRef>? = nil

    let couldAddPerson = ABAddressBookAddRecord(inAddressBook, person, &error)

    if couldAddPerson {
        println("Added person")
    } else{
        println("Failed to add person")
        return nil
    }

    if ABAddressBookHasUnsavedChanges(inAddressBook){

        var error: Unmanaged<CFErrorRef>? = nil
        let couldSaveAddressBook = ABAddressBookSave(inAddressBook, &error)

        if couldSaveAddressBook{
            println("Saved address book")
        } else {
            println("Failed to save address book")
        }

        if couldSetFirstName && couldSetLastName {
            println("Succesfully set first and last name")
        } else{
            println("Failed to set first and last name")
        }
    }

    return person
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Niels Robben
  • 1,637
  • 4
  • 17
  • 24

1 Answers1

2

You're passing a string to set the value of kABPersonPhoneProperty, which is not correct. The phone number is not a string property; it's a multi-value property. You need to set it using something like the code from here: How to create contacts in address book in iPhone SDK? (which is Objective-C, but should be straightforward to translate.)

NSString *phone = @"0123456789"; // the phone number to add

//Phone number is a list of phone number, so create a multivalue    
ABMutableMultiValueRef phoneNumberMultiValue = ABMultiValueCreateMutable(kABMultiStringPropertyType);
ABMultiValueAddValueAndLabel(phoneNumberMultiValue, phone, kABPersonPhoneMobileLabel, NULL);

ABRecordSetValue(person, kABPersonPhoneProperty, phoneNumberMultiValue, &anError); // set the phone number property
Community
  • 1
  • 1
Jesse Rusak
  • 56,530
  • 12
  • 101
  • 102
  • Hi Jesse, thanks for the response! I have translated that snipped to Swift, which is almost identical. Unfortunately it gives one error at the first line. Instead of the "kABPersonPhoneProperty" as parameter, it is asking for an "ABPropertyType". Any idea how to get this? – Niels Robben Jan 15 '15 at 15:44
  • @NielsRobben Yep, sorry; there was a bug in the original answer which I've fixed; the argument there is the kind of multi-value property to create, which the docs say is `kABMultiStringPropertyType` for the phone field. – Jesse Rusak Jan 15 '15 at 15:50
  • Thanks! It is still not working though, the problem is, according to xcode, that an int is not convertible to 'ABPropertyType'. The ABPropertyType's return type is somehow Uint32.. Appreciating your help! – Niels Robben Jan 15 '15 at 16:00
  • @NielsRobben This looks like a bug in the swift headers for Address Book; you need to do something like `ABMultiValueCreateMutable(ABPropertyType(kABMultiStringPropertyType))`. – Jesse Rusak Jan 15 '15 at 16:04
  • Awesome, that fixes it in combination with requesting the unretained value of the multivalue property upon saving. Thanks for your help! – Niels Robben Jan 15 '15 at 16:08
  • Could you post the final version of the answer in Swift 2? – Matt Nov 24 '15 at 19:25