0

I have created a custom property in AddressBook named "Qref". I can check it's there using [ABPerson properties], and it's always there for any test app I write.

By the way, you can't remove custom properties, because [ABPerson removeProperties] hasn't been implemented. Let me know if it ever is, because I need to remove one whose name I mistyped.

I set a property value with this code.

ABPerson *p = <person chosen from a PeoplePicker>;
NSError *e;
if (![p setValue: aString forProperty:@"Qref" error:&e]) {
  [NSAlert alertWithError:e]runModal;
}

(I have never seen the alert yet, but sometimes get a heap of error messages in the console.) At this point I can navigate away from the person in the PeoplePicker and return to find the value correctly set. If I check [[ABAddressBook sharedAddressBook] hasUnsavedChanges] the result is NO, so clearly changing a custom property value doesn't count as a change, so I force a save by inserting dummy person (please suggest a better way), then executing

[[ABAddressBook sharedAddressBook] save];

The dummy person appears immediately in AddressBook if it is running, so something's right. But when I close my app and run it again, I find the values I set have gone. (MacOSX-Lion)

AlexT
  • 596
  • 7
  • 15

1 Answers1

1

I've been barking up the wrong trees. It's turning out that I couldn't save any properties, irrespective of whether they were custom ones or not. Then I wondered if it was something to do with code-signing, entitlements or iCloud, which is impenetrable jungle to me. It seems the person you get from a PeoplePicker isn't associated with any address book so [[ABAddressBook sharedAddressBook] save] will do nothing. You have to get your person from an ABAddressBook instance. Here's the skeleton of what works, without any error checking.

ABPerson *p = [[myPeoplePicker selectedRecords] objectAtIndex:0];
NSString *uid = p.uniqueId;
ABPerson *editablePerson = 
     (ABPerson*) [[ABAddressBook sharedAddressBook] recordForUniqueId:uid];
// Typecast because it returns an ABRecord. Can anyone improve?
[editablePerson setValue:@"ABCD" forProperty:@"Qref"]; // my custom property
[[ABAddressBook sharedAddressBook] save];
AlexT
  • 596
  • 7
  • 15
  • You're a lifesaver! ABPerson, ABPersonView and the ABAddressBook on Mac OS X have a lot of hidden gotchas that have yet to be documented on the web. I was beginning to think no one else was using these new 10.7 classes. – Byron Apr 27 '12 at 03:12
  • I am referring to the new Address Book classes available in Lion. – Byron Apr 27 '12 at 03:32