0

I'm facing issue while adding new contact and getting record id in didCompleteWithNewPerson. The record id gets automatically changed and i'm not able to get contact detail from that record id.

Here is the code i'm using to get contact detail from record id.

ABAddressBookRef myAB = ABAddressBookCreateWithOptions(NULL, NULL);
// Get the person record from the address book
ABRecordRef person = ABAddressBookGetPersonWithRecordID(myAB,(int) passedABRef);

NSLog(@"passedABRef == %ld",(long)passedABRef);

if (person)
{
    CFStringRef cfName = ABRecordCopyValue(person, kABPersonFirstNameProperty);
    if (cfName) {
        thePerson.pFirstName = (NSString *)cfName;
        CFRelease(cfName);
    } else {
        thePerson.pFirstName = @"";
    }
}
James Z
  • 12,209
  • 10
  • 24
  • 44
Shah Paneri
  • 729
  • 7
  • 28
  • Unless you're trying to support iOS 8 or earlier, you really shouldn't be using AddressBook framework. You should use Contacts framework. – Rob Jan 08 '18 at 14:36
  • It makes me nervous to see you casting the `passedABRef`. Why are you doing that? You really shouldn't be doing any casting. Also, is `person` `NULL` or not? Was `cfName` `NULL` or not? Was `thePerson` `nil` or not? Where did you instantiate `thePerson`? How was `pFirstName` declared? You have to show us more precise debugging information if you want us to help you. – Rob Jan 08 '18 at 14:45
  • Also, was `myAB` non-NULL, too? I.e. you'd generally declare `CFErrorRef error = NULL;` and then pass `&error` as the second parameter to `ABAddressBookCreateWithOptions` and if `myAB` was `NULL`, you'd then look at `NSLog(@"ABAddressBookCreateWithOptions error: %@", CFBridgingRelease(error));`. E.g. Did you `ABAddressBookRequestAccessWithCompletion` before you tried to use the address book? Did you set `NSContactsUsageDescription` in the `Info.plist`? – Rob Jan 08 '18 at 14:55

0 Answers0