I know how to open up and send an email in iOS using MFMailComposeViewController
. The code below will let the user write and send an email, and then go straight back to the app.
- (IBAction)sendEmail:(id)sender {
// Email Subject
NSString *emailTitle = @"Test";
// Email Content
NSString *messageBody = @"Test";
// To address
NSArray *toRecipents = [NSArray arrayWithObject:@"te@gmail.com"];
MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
mc.mailComposeDelegate = self;
[mc setSubject:emailTitle];
[mc setMessageBody:messageBody isHTML:NO];
[mc setToRecipients:toRecipents];
// Present mail view controller on screen
[self presentViewController:mc animated:YES completion:NULL];
}
- (void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
switch (result)
{
case MFMailComposeResultCancelled:
NSLog(@"Mail cancelled");
break;
case MFMailComposeResultSaved:
NSLog(@"Mail saved");
break;
case MFMailComposeResultSent:
NSLog(@"Mail sent");
break;
case MFMailComposeResultFailed:
NSLog(@"Mail sent failure: %@", [error localizedDescription]);
break;
default:
break;
}
// Close the Mail Interface
[self dismissViewControllerAnimated:YES completion:NULL];
}
What I'm struggling to figure out is if I want to have the user open up the address book and send an email to one of there contacts, after the email is sent, the user has to re-open the app...and I'd like them to return to the app automatically. Below is the code for opening up the address book, viewing a contact, and either sending an email/making a call/sending a message to that contact.
- (IBAction)openContacts:(id)sender {
ABPeoplePickerNavigationController *peoplePicker =
[[ABPeoplePickerNavigationController alloc] init];
peoplePicker.peoplePickerDelegate = self;
[self presentModalViewController:peoplePicker animated:YES];
}
- (BOOL)peoplePickerNavigationController:
(ABPeoplePickerNavigationController *)picker
shouldContinueAfterSelectingPerson:(ABRecordRef)person
{
// [self dismissModalViewControllerAnimated:YES];
return YES;
}
- (BOOL)peoplePickerNavigationController:
(ABPeoplePickerNavigationController *)picker
shouldContinueAfterSelectingPerson:(ABRecordRef)person
property:(ABPropertyID)property
identifier:(ABMultiValueIdentifier)identifier
{
return YES;
}
- (void)peoplePickerNavigationControllerDidCancel:
(ABPeoplePickerNavigationController *)picker
{
[self dismissViewControllerAnimated:YES completion:NULL];
}
Does anyone know to have the user return straight back to the app after opening the address book? Thanks!
EDIT After implementing @Rob's suggestion, my code looks like this:
- (IBAction)openContacts:(id)sender {
ABPeoplePickerNavigationController *peoplePicker =
[[ABPeoplePickerNavigationController alloc] init];
peoplePicker.peoplePickerDelegate = self;
[self presentModalViewController:peoplePicker animated:YES];
}
- (BOOL)peoplePickerNavigationController:
(ABPeoplePickerNavigationController *)peoplePicker
shouldContinueAfterSelectingPerson:(ABRecordRef)person
{
return YES;
}
- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker
didSelectPerson:(ABRecordRef)person
property:(ABPropertyID)property
identifier:(ABMultiValueIdentifier)identifier {
if (property == kABPersonEmailProperty) {
ABMultiValueRef emails = ABRecordCopyValue(person, property);
CFIndex index = ABMultiValueGetIndexForIdentifier(emails, identifier);
NSString *email = CFBridgingRelease(ABMultiValueCopyValueAtIndex(emails, index));
CFRelease(emails);
MFMailComposeViewController *composeViewController = [[MFMailComposeViewController alloc] init];
[composeViewController setToRecipients:@[email]];
composeViewController.mailComposeDelegate = self;
[peoplePicker dismissViewControllerAnimated:NO completion:^{
[self presentViewController:composeViewController animated:YES completion:nil];
}];
} else {
[peoplePicker dismissViewControllerAnimated:YES completion:nil];
}
}
- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker {
[peoplePicker dismissViewControllerAnimated:YES completion:nil];
}
I'm able to select a contact and either make a phone call or email, but still have to return to the app manually after completing one of these actions.