iosabaddressbookaddressbookui

IOS: Is there an easy way to format a contact's available names into a string the way the user prefers?


I need to display some sort of user friendly string that identifies a person from their address book record. Since the person records can have any combination of first name, last name, nickname, middle name and there can be any combination of user preferences for displaying them (shorten first or last name, prefer nicknames, last names first, etc.) it is quickly becoming a huge task to do by myself.

Is there a routine I haven't found that will do this formatting for me?

E.g. something like:

NSString *displayName = (__bridge NSString*)APIGetDisplayNameForPersonRecord(self.selectedPerson);

Instead of:

NSString *firstName = (__bridge_transfer NSString*) ABRecordCopyValue(self.selectedPerson, kABPersonFirstNameProperty);
NSString *lastName = (__bridge_transfer NSString*) ABRecordCopyValue(self.selectedPerson, kABPersonLastNameProperty);
NSString *nickname = (__bridge_transfer NSString*) ABRecordCopyValue(self.selectedPerson, kABPersonNicknameProperty);

NSString *displayName = @"";
if(firstName) displayName = firstName;
if(lastName) // ...
if(nickname) // ...
// lots of logic

Solution

  • The closest I could find was ABRecordCopyCompositeName() which is good enough.