Why not present the address book and let the user choose and email address?
Add the AddressBook and AddressBookUI frameworks to your project.
Import them in your .h and add the protocol ABPeoplePickerNavigationControllerDelegate.
Then call the adress book:
- (void) chooseContact:(id)sender {
ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init];
picker.peoplePickerDelegate = self;
[self presentModalViewController:picker animated:YES];
[picker release];
}
You implement several required delegate methods to get an email address and dismiss the Address Book:
// call when the user cancel
- (void) peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker {
[self dismissModalViewControllerAnimated:YES];
}
Let the user enter in contact details:
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person {
return YES;
}
Then do what you you with the email address selected:
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier {
if (property == kABPersonEmailProperty) {
//assumed you have a property email in your class
self.email = (NSString *)ABRecordCopyValue(person, kABPersonEmailProperty);
[self dismissModalViewControllerAnimated:YES];
}
return NO;
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…