Showing posts tagged development

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);
}

Usability fail: On the right side, there are three entry fields all directly next to each other, each performing different actions.

On the plus side, they at least have some prompting text in them to tell me what they do, but that doesn’t help that much. I still have to read each of them, and consider each one, before deciding which to use. I continually search for addresses instead of messages, accidentally, for example.

As I’ve said for a few years now, any UI you have to think about has already failed.

Side Benefits

Advantage to writing code for yourself: funny method names. And no one can get mad at you.

- (void)carryOn // Continue
- (void)kImOut  // Go back 

I really amuse myself.