问题
I'm trying to create a new record of Person thru my App. I have the name, email and phone nr. How can i pass them to the modal view of newPerson? I'm following Apple's docs, but i'm stuck. I'm using a ABNewPersonViewController. Is that correct? How do I fill the fields in the modal view?
Thanks,
RL
回答1:
If you want to display something in the ABNewPersonViewController, you have to create and set up an ABRecordRef for the properties you want to pre-fill and then set the displayedPerson property on ABNewPersonViewController. So a simple example may be:
- (void)createNewPerson
{
// Create the pre-filled properties
ABRecordRef newPerson = ABPersonCreate();
CFErrorRef error = NULL;
ABRecordSetValue(newPerson, kABPersonFirstNameProperty, CFSTR("John"), &error);
ABRecordSetValue(newPerson, kABPersonLastNameProperty, CFSTR("Smith"), &error);
NSAssert( !error, @"Something bad happened here." );
// Create and set-up the new person view controller
ABNewPersonViewController* newPersonViewController = [[ABNewPersonViewController alloc] initWithNibName:nil bundle:nil];
[newPersonViewController setDisplayedPerson:newPerson];
[newPersonViewController setNewPersonViewDelegate:self];
// Wrap in a nav controller and display
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:newPersonViewController];
[self presentModalViewController:navController animated:YES];
// Clean up everything
[navController release];
[newPersonViewController release];
CFRelease(newPerson);
}
回答2:
if you already have the details and you just want to write them to the address book then you don't need the - ABNewPersonViewController. the ABNewPersonViewController is used for letting the user to add record to the address book with the common controller of the iphone address book.
instead you should do the following:
a) add the address book () frame work to your app.
b) import the address book headers to your view.
c) create an addressbook instanse and a new record:
ABAddressBookRef iPhoneAddressBook = ABAddressBookCreate();
ABRecordRef newPerson = ABPersonCreate();
d) for single value properties you use:
ABRecordSetValue(newPerson, kABPersonFirstNameProperty, CFSTR("Katie"), nil);
ABRecordSetValue(newPerson, kABPersonLastNameProperty, CFSTR("Bell"), nil);
e)for the multy value properties such as phone number / mail you use:
ABMutableMultiValueRef multiPhone = ABMultiValueCreateMutable(kABMultiStringPropertyType);
ABMultiValueAddValueAndLabel(multiPhone, @"054-5429920", kABHomeLabel, NULL);
ABMultiValueAddValueAndLabel(multiPhone,@"02-9809878", kABWorkLabel, NULL);
ABRecordSetValue(newPerson, kABPersonPhoneProperty, multiPhone,nil);
CFRelease(multiPhone);
ABMutableMultiValueRef multiEmail = ABMultiValueCreateMutable(kABMultiStringPropertyType);
ABMultiValueAddValueAndLabel(multiEmail, @"shannoga@me.com", kABHomeLabel, NULL);
ABRecordSetValue(newPerson, kABPersonEmailProperty, multiEmail, &error);
CFRelease(multiEmail);
f) save the changes to the address book, none of the code you wrote wond take effect with out this:
ABAddressBookAddRecord(addressBook, newPerson, nil);
BOOL didAdd = ABAddressBookSave(addressBook, nil);
g) relese the record and the address book and check if the record addaed:
CFRelease(newPerson);
CFRelease(addressBook);
NSLog(@"didAdd = %d",didAdd);
thats all good luck hope it helped
shani
来源:https://stackoverflow.com/questions/4484481/iphone-addressbook-how-to-create-a-new-record-with-app-from-my-app