【发布时间】:2014-10-19 23:36:00
【问题描述】:
我知道如何在 iOS 中使用 MFMailComposeViewController 打开和发送电子邮件。下面的代码将让用户编写和发送电子邮件,然后直接返回应用程序。
- (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];
}
我想弄清楚的是,如果我想让用户打开通讯簿并向其中一个联系人发送电子邮件,则在发送电子邮件后,用户必须重新打开应用程序。 ..我希望他们自动返回应用程序。以下是用于打开通讯录、查看联系人以及向该联系人发送电子邮件/拨打电话/发送消息的代码。
- (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];
}
有谁知道让用户在打开通讯录后直接返回应用程序?谢谢!
编辑 执行@Rob 的建议后,我的代码如下所示:
- (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];
}
我可以选择联系人并拨打电话或发送电子邮件,但在完成其中一项操作后仍需要手动返回应用程序。
【问题讨论】:
-
谢谢,刚刚解决了这个问题!
标签: ios objective-c mfmailcomposeviewcontroller abpeoplepickerview