iosswiftcore-datansnumberabpeoplepickerview

Convert ABRecordID to NSNumber always -1


I am trying to save an ABRecordID of an existing contact from the Address Book into Core Data. I have an Int32 defined in my model, but in order to save my object there I need to convert the ABRecordID (which is an Int32) into an NSNumber

Here is my code that converts it:

func peoplePickerNavigationController(peoplePicker: ABPeoplePickerNavigationController!, didSelectPerson person: ABRecord!, property: ABPropertyID, identifier: ABMultiValueIdentifier) {
    let multiValue: ABMultiValueRef = ABRecordCopyValue(person, property).takeRetainedValue()
    let index = ABMultiValueGetIndexForIdentifier(multiValue, identifier)
    let pickedVal = ABMultiValueCopyValueAtIndex(multiValue, index).takeRetainedValue() as! String

    let theRecord = recordIdFromPersonRecord(person)
    println(ABRecordGetRecordID(person!))

func recordIdFromPersonRecord (personRecord: ABRecord) -> NSNumber {
    let recordId = ABRecordGetRecordID(personRecord)
    println(personRecord)
    return (NSNumber(int: recordId))
}

This code always prints -1 in the console, no matter what record I select in my address book. I'm a newbie programmer and I'm totally stuck.

Thanks for your help!

EDIT: Commenter below indicated that this is a value reserved for records that do not exist in the address book database...

That is confusing. I am just browsing the AB to get the name value and the ID out of existing records... how can existing records not have a record saved to the address book database? Here is what the definition from the docs is: Records with this ID have not been saved to the Address Book database. Am I misunderstanding?


Solution

  • In iOS 8, you can browse the contacts without it ever requesting permission to the address book (because you might, for example, allow the user to perform the default action with ever returning actual data to the app). But to actually get the particulars on a contact, you have to explicitly request permission to do so.

    It would appear that you must have used AddressBookUI.framework to present the contact UI, but never asked for permission to access the particulars about the contact.