Cocoa Tip: Adding an IM username to a contact
I spent an inordinate amount of time yesterday playing with the iPhone app I’m working on trying to figure out how to get an IM username into a new contact to be added to the device’s address book. I couldn’t find any good documentation about it online, perhaps because it’s immediately apparent to everyone but me. Nonetheless, I thought I should mention it somewhere on the Internet so that hopefully the oracle can find it.
Regardless, here’s the snippet for a Yahoo! username, not accounting for gross but necessary things like memory management or cleanliness:
ABRecordRef entry = ABPersonCreate();
CFErrorRef error = NULL;
ABMutableMultiValueRef im = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType);
NSMutableDictionary *imDict = [[NSMutableDictionary alloc] init];
[imDict setObject:(NSString*)kABPersonInstantMessageServiceYahoo forKey:(NSString*)kABPersonInstantMessageServiceKey];
[imDict setObject:@"REPLACE THIS STRING WITH THE USERNAME"] forKey:(NSString*)kABPersonInstantMessageUsernameKey];
ABMultiValueAddValueAndLabel(im, imDict, kABWorkLabel, NULL);
[imDict release];
ABRecordSetValue(entry, kABPersonInstantMessageProperty, im, &error);
if (error != NULL)
{
NSLog(@"Could not add yahoo: %@", error);
}