Datasheet

Book VII
Chapter 1
A User Interface
for Adding Hotels
and Using the
Address Book
783
Interfacing with the Address Book Application
Listing 1-23: Done with the Record
- (void)newPersonViewController:
(ABNewPersonViewController *)newPersonViewController
didCompleteWithNewPerson:(ABRecordRef)person {
[self dismissModalViewControllerAnimated:YES];
}
Now they you’ve gotten that out of the way, you can concentrate on what you
need to do to actually add the new contact. Adding a contact to the iPhone’s
Address Book isn’t horribly complicated, but there’s some work to do.
To start, you need to implement the IBAction saveToContacts: method —
that’s where the work will get done.
Add the code in Listing 1-24 to AddHotelController.m.
Listing 1-24: saveToContacts:
- (IBAction) saveToContacts:(id)sender{
ABAddressBookRef addressBook = ABAddressBookCreate();
ABRecordRef personRecord = ABPersonCreate();
ABRecordSetValue(personRecord, kABPersonOrganizationPropert
y,name.text, nil);
ABRecordSetValue(personRecord,
kABPersonLastNameProperty,name.text,nil);
ABMutableMultiValueRef mutableMultiValueRef =
ABMultiValueCreateMutable
(kABMultiDictionaryPropertyType);
NSMutableDictionary *addressDictionary =
[[NSMutableDictionary alloc] init];
[addressDictionary setObject:street.text forKey:(NSString
*) kABPersonAddressStreetKey];
[addressDictionary setObject:city.text
forKey:(NSString *)kABPersonAddressCityKey];
[addressDictionary setObject:state.text
forKey:(NSString *)kABPersonAddressStateKey];
[addressDictionary setObject:zip.text
forKey:(NSString *)kABPersonAddressZIPKey];
ABMultiValueAddValueAndLabel(mutableMultiValueRef,
addressDictionary, kABWorkLabel, nil);
ABRecordSetValue(personRecord, kABPersonAddressProperty,
mutableMultiValueRef, nil);
CFRelease(mutableMultiValueRef);
ABAddressBookAddRecord(addressBook, personRecord, nil);
ABAddressBookSave(addressBook, nil);
(continued)
41_542934-bk07ch01.indd 78341_542934-bk07ch01.indd 783 3/23/10 11:01 PM3/23/10 11:01 PM