8

I have an application that adds Contacts to the Address Book in iOS, and all I need is to store the ABRecordID or ABRecordref as soon as I save the contact.

ABAddressBookAddRecord(addressbook, newPerson, &theerror);
ABAddressBookSave(addressbook, &theerror);

This question is extremely similar, but I don't understand the answer: ABRecordID for a record in addressbook(unique id for inserted record in addressbook)

How do you know the

ABRecordID ABRecordGetRecordID (
ABRecordRef newPerson
);

Some help here would be fantastic - thanks!

Community
  • 1
  • 1
Tommy Nicholas
  • 1,133
  • 5
  • 20
  • 31

1 Answers1

19

Simply call ABRecordGetRecordID(newPerson) after you've saved the address book (before that, there won't be a valid ID).

//...
ABRecordID recordID = ABRecordGetRecordID(newPerson);

An ABRecordID is the same as an int_32 (a 32-bit integer).

omz
  • 53,243
  • 5
  • 129
  • 141
  • That's the ticket! Thanks omz you da man. Gonna accept your answer in 2 minutes when I'm allowed. – Tommy Nicholas Jul 27 '13 at 22:58
  • Its always giving -1 as ABRecordID with native contact picker ABRecordRef in ios 8.1 – Abhishek Jan 21 '15 at 08:14
  • 1
    @Abhishek this is a complete guess but maybe since you don't get contact permission from the user when using the ios 8 native contact picker apple is hiding the record id by always returning it as -1? – danny Jan 26 '15 at 01:36
  • @danny Yes you are right, iOS 8 create copy of contact picked, so person reference is not available in db and hence ABRecordGetRecordID provide -1 – Abhishek Jan 27 '15 at 05:35
  • @Abhishek Hi. I am stuck in getting ABRecordGetRecordID in swift. I am always getting 1. Can you please help me. I need it very badly. – Ekta Padaliya Jan 13 '17 at 11:10
  • @EktaPadaliya With current iOS, you can always use CNContact and it will provide you identifier as well which is unique among contacts on the device. – Abhishek Jan 17 '17 at 05:43
  • @Abhishek So identifier and ABRecordGetRecordID is same right? – Ekta Padaliya Jan 17 '17 at 05:44
  • 2
    @EktaPadaliya First- ABRecordGetRecordID is available iOS2.0- iOS9.0, ABAddressBook (ABRecordGetRecordID) - The unique ID of record when the record exists in the Address Book database., CNContact (identifier) - The identifier is unique among contacts on the device. It can be saved and used for fetching contacts next application launch. Copied from developer.apple :) – Abhishek Jan 17 '17 at 11:05
  • @EktaPadaliya yes this looks similar to RecordId – Abhishek Jan 17 '17 at 11:10