Datasheet
784
Interfacing with the Address Book Application
Listing 1-24 (continued)
ABNewPersonViewController *picker =
[[ABNewPersonViewController alloc] init];
picker.newPersonViewDelegate = self;
picker.displayedPerson = personRecord;
UINavigationController* navigationController =
[[UINavigationController alloc] initWithRootViewController
:picker];
[self presentModalViewController:navigationController
animated:YES];
[picker release];
}
To create a new Address Book entry, start by creating a new Address Book
with data from the Address Book database.
ABAddressBookRef addressBook = ABAddressBookCreate();
When you have the Address Book, you’ll create the new record you want to
add to it.
ABRecordRef personRecord = ABPersonCreate();
Then you’ll add the data to the new record.
For single-value rewords, like name, you’ll do something similar to what
you did when you accessed the Address Book information back in the
last section. But in this case, instead of ABRecordCopyValue you’ll use
ABRecordSetValue. (Kind of makes sense doesn’t it?)
ABRecordSetValue(personRecord, kABPersonOrganizationProperty,
name.text, nil);
ABRecordSetValue(personRecord,
kABPersonLastNameProperty,name.text, nil);
This code sets the Organization and Last Name fields with the text from the
Name field in the view. The last argument is a place to return any errors, but
throughout this code I use nil — but feel free to explore that on your own.
Next in line in Listing 1-24 is adding a new address record. (I’ll skip the
phone number multi-value property type — but you get the drift.) To add a
new address record, you create a new dictionary property type and then a
new dictionary. In this case, you’re creating an ABMutableMultiValueRef
instead of the multiValueRef you used when you read the contact infor-
mation in the previous section. It needs to be mutable because you’re going
to making changes to it.
41_542934-bk07ch01.indd 78441_542934-bk07ch01.indd 784 3/23/10 11:01 PM3/23/10 11:01 PM