It’s left quite a negative impression from last time I faced with plain C/C++ like code. That was a necessity to use huge Boost library with all related problems. As they say – “Nothing personal”, it works pretty fast and code event elegantly somewhere, but an instrument is effective only when you know it well enough. Or at least you have time and wish to learn it. That’s not my case. I’d not say the Boost is easy to adapt for objective-c users, rather it isn’t.
Since then I try to avoid plain C/C++, especially when it based on huge template hierarchies. But sometimes somewhere the numerous iOS API we should have to write C like code.
This time I had to assign an url (or any other information) to existing contact from AddressBook. I was surprised to see such a non–objectivec API. Hope that’s simply a necessary, but I’m sure it’s a “thing from the past”. Since it has taken more than 5 mins as it shouldn’t to be, I want share that piece of code:
Show dialog to select an existing person (contact):
- (void) showAddressBook{ ABPeoplePickerNavigationController *picker =
[[ABPeoplePickerNavigationController alloc] init]; picker.peoplePickerDelegate = self;
[self presentModalViewController:picker animated:YES]; [picker release];}
Some delegate’s methods:
- (void)peoplePickerNavigationControllerDidCancel: (ABPeoplePickerNavigationController *)
peoplePicker
{ [self dismissModalViewControllerAnimated:YES];} -
(BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController*) peoplePicker
shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)
property
identifier:(ABMultiValueIdentifier)identifier{ return NO;}
and the main part – add new field with string to selected person profile:
- (BOOL)peoplePickerNavigationController :(ABPeoplePickerNavigationController *)
peoplePicker shouldContinueAfter
SelectingPerson:(ABRecordRef)person { CFErrorRef anError =
NULL; ABMutableMultiValueRef urls = ABRecordCopyValue
(person, kABPersonURLProperty); urls = ABMultiValueCreateMutableCopy(urls);
ABMultiValueAddValueAndLabel(urls, @"Marked programmatically",
CFSTR("Info"), NULL); ABRecordSetValue(person, kABPersonURLProperty,
urls, &anError);if (anError) { return NO; } CFRelease(urls) ;
ABAddr essBookSave (peoplePicker.
addressBook, NULL);
[self dismissModalViewControllerAnimated:YES]; return NO;}